【发布时间】:2021-07-18 09:01:13
【问题描述】:
我正在使用 Django 构建应用程序,我想使用 django-import-export 从 Excel 文件中导入数据。
导入数据时,我想跳过未更改的行,为此,我在资源类中使用skip_unchanged = True(如下所示),但出现意外行为。在我的模型中,我有一个属性updated_at,它是一个带有auto_now=True 属性的DateTimeField,即使文件中的行值没有更改,每次我上传Excel 文件时它都会采用一个新值。
以下是我的部分代码。
models.py
class HREmployee(models.Model):
code = models.IntegerField()
name_en = models.CharField(max_length=55)
status = models.CharField(max_length=75)
termination_date = models.DateField(null=True)
hiring_date = models.DateField()
birth_date = models.DateField()
# other fields to be imported from the file ...
# fields that I want to use for some purposes (not imported from the file)
comment = models.TextField()
updated_at = models.DateTimeField(auto_now=True)
resources.py
class HREmployeeResource(ModelResource):
code = Field(attribute='code', column_name='Employee Code')
name_en = Field(attribute='name_en', column_name='Employee Name - English')
status = Field(attribute='status', column_name='Employee Status')
termination_date = Field(attribute='termination_date', column_name='Termination Date')
hiring_date = Field(attribute='hiring_date', column_name='Hiring Date')
birth_date = Field(attribute='birth_date', column_name='Birth Date')
# other fields to be imported ...
class Meta:
model = HREmployee
import_id_fields = ('code', )
skip_unchanged = True
那么,谁能帮我解决这个意外行为?
编辑
经过几次尝试,我发现带有日期值的列导致了这个问题。
在 Excel 文件中,我有三列具有如下图所示的日期值,当我在资源类中注释相应的属性并进行导入时,我得到了预期的行为(如果文件中没有更改import_type 等于 skip 并且在 DB 中没有进行任何更改)。
我已经编辑了模型和资源类的代码(请在上面查看)。
【问题讨论】:
标签: django django-import-export