Я пытаюсь импортировать три набора данных для трех взаимосвязанных моделей данных, и я не могу правильно добавить внешний ключ.Когда я пытаюсь использовать функцию fields.Feild () для создания внешнего ключа, я получаю имя ошибки в этой строке: hid = fields.Field (column_name = 'hid', attribute = 'hid', ..).Пожалуйста, помогите, я пытался загрузить свои наборы данных в течение недели.Первоначально я пытался сделать это вручную, но не смог, поэтому теперь я использую этот пакет django-import-export.
NameError: name 'fields' is not defined
Вот мой models.py
class Service(models.Model):
"""Model representing an author."""
serviceid = models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular service in database')
desc_us = models.TextField(blank=True, primary_key = True)
cpt = models.IntegerField(default= 10000)
price = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
_str__(self):
"""String for representing the Model object."""
return self.desc_us
# Create your models here.
class Library(models.Model):
"""Model representing Librarys."""
hid = models.CharField(max_length = 8, null=True)
name = models.CharField(max_length=200, primary_key=True)
hopid = models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular library in database')
address = models.CharField(max_length = 200, null = True)
city = models.CharField(max_length = 50, null = True)
state = models.CharField(max_length = 2, null=True)
zipcode = models.CharField(max_length = 5, null=True)
phone = models.CharField(max_length = 12, null=True)
updateDate = models.DateField(blank=True, null=True)
class Meta:
ordering = ['hopid']
def __str__(self):
"""String for representing the Model object."""
return f'{self.name} ({self.address})'
class Price(models.Model):
"""Model with all the hospital prices by service."""
priceid = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular service in database')
com_desc = models.CharField(max_length = 200, blank = True, null = True)
service = models.ForeignKey("Service", on_delete=models.SET_NULL, null=True)
price_offer = models.DecimalField(max_digits=8, decimal_places=2, blank=True)
comments = models.CharField(max_length = 200, blank = True, null =True)
hospital = models.ForeignKey("Hospital", on_delete=models.SET_NULL, null=True)
class Meta:
ordering =['service']
def __str__(self):
return f'{self.hospital.name} ({self.service.desc_us}) ({self.price_offer})'
Вот admin.py
from import_export.admin import ImportExportModelAdmin
from import_export import resources
class ServiceResource(resources.ModelResource):
class Meta:
model = Service
report_skipped = True
exclude = ('id','serviceid')
import_id_fields = ('desc_us', 'cpt', 'price',)
@admin.register(Service)
class ServiceAdmin(ImportExportModelAdmin):
resource_class = ServiceResource
class LibraryResource(resources.ModelResource):
class Meta:
model = Library
report_skipped = True
exclude = ('id','hopid','updateDate')
import_id_fields = ('hid', 'name', 'address', 'city', 'state', 'zipcode', 'phone',)
@admin.register(Library)
class LibraryAdmin(ImportExportModelAdmin):
resource_class = LibraryResource
class PriceResource(resources.ModelResource):
hid = fields.Field(column_name='hid',attribute='hid', widget=ForeignKeyWidget(Library, 'name'))
class Meta:
model = Price
report_skipped = True
exclude = ('id','priceid')
@admin.register(Price)
class PriceAdmin(ImportExportModelAdmin):
list_display = ('com_desc', 'cpt', 'price_offer', 'comments', 'hid',)
resource_class = PriceResource