使用2个字段作为ID字段导入

时间:2019-01-18 19:31:52

标签: django django-import-export

我已经阅读了django import_export的文档,并注意到我们可以使用import_id_fields导入将哪个字段用作导入ID。我想知道是否可以使用字段组合。

例如,我有一个模型选择字段月份和名称,因此在导入期间,它需要两个字段并将其用作导入ID?

P.S我为我的英语损坏(这是我的第三种语言)表示歉意

models.py:

class Book(models.Model):
    JANUARY = '1'
    FEBRUARY = '2'
    MARCH = '3'
    APRIL = '4'
    MAY = '5'
    JUNE = '6'
    JULY = '7'
    AUGUST = '8'
    SEPTEMBER = '9'
    OCTOBER = '10'
    NOVEMBER = '11'
    DECEMBER = '12'

    MONTH_CHOICES = (
        (JANUARY, 'January'),
        (FEBRUARY, 'February'),
        (MARCH, 'March'),
        (APRIL, 'April'),
        (MAY, 'May'),
        (JUNE, 'June'),
        (JULY, 'July'),
        (AUGUST, 'August'),
        (SEPTEMBER, 'September'),
        (OCTOBER, 'October'),
        (NOVEMBER, 'November'),
        (DECEMBER, 'December'),
    )

    name = models.CharField('Book name', max_length=100)
    author = models.ForeignKey(Author, models.SET_NULL, blank=True, null=True)
    author_email = models.EmailField('Author email', max_length=75, blank=True)
    imported = models.BooleanField(default=False)
    published = models.DateField('Published', blank=True, null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    categories = models.ManyToManyField(Category, blank=True)
    month = models.CharField(
        max_length=2,
        choices= MONTH_CHOICES,
        default=JANUARY,
    )


    def __str__(self):
        return self.name

admin.py: 


# Register your models here.
class BookResource(resources.ModelResource):

    class Meta:
        model = Book
        import_id_fields = ('name',)
        fields = ('name', 'price','month')



@admin.register(Book)
class BookAdmin(ImportExportActionModelAdmin):
    resource_class = BookResource

1 个答案:

答案 0 :(得分:1)

Yes it looks like you can.我相信您的资源模型应如下所示:

testİmplementation