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

Import using 2 fields as id field

我已经阅读了 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

Yes it looks like you can. 我相信你的资源模型应该是这样的:

class BookResource(resources.ModelResource):
    class Meta:
        model = Book
        import_id_fields = ('name', 'month')
        fields = ('name', 'price','month')