django csv进出口软件包的外键问题

时间:2019-03-03 14:21:45

标签: django django-models django-import-export

我正在尝试为三个互连的数据模型导入三个数据集,但似乎无法正确添加外键。当我尝试使用fields.Feild()函数创建外键时,在此行中出现名称错误:hid = fields.Field(column_name ='hid',attribute ='hid',..)。请帮助,我已经尝试上传我的数据集一周了。我最初尝试手动进行操作,但无法执行操作,因此现在我使用的是django-import-export程序包。

NameError: name 'fields' is not defined

这是我的模特。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

1 个答案:

答案 0 :(得分:1)

在顶部添加:

start = 0
end = 3

ans = []
for i in range(start, end):
    if i == start:
        ans.append("*" + "-" * 2)
        continue
    if i == end-1:
        ans.append("-" * 2 + "*")
        continue

    ans.append("-" * 3)

print(ans)

然后将您的代码行更改为:

import import_export

您目前没有告诉Django在哪个库中查找“字段”,所以它不知道。