Django Import-export TypeError: clean() got an unexpected keyword argument 'row'

Django Import-export TypeError: clean() got an unexpected keyword argument 'row'

导入 csv 文件时出现以下错误。我认为这与日期小部件有关?

这是我的 csv 示例:

" , ICE, France, EST Current, HD, 4.59, EUR, GROSS, 01/01/08, test"

Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/import_export/resources.py", line 662, in import_row
self.import_obj(instance, row, dry_run)
File "/usr/local/lib/python3.6/site-packages/import_export/resources.py", line 516, in import_obj
self.import_field(field, obj, data)
File "/usr/local/lib/python3.6/site-packages/import_export/resources.py", line 499, in import_field
field.save(obj, data, is_m2m)
File "/usr/local/lib/python3.6/site-packages/import_export/fields.py", line 110, in save
cleaned = self.clean(data)
File "/usr/local/lib/python3.6/site-packages/import_export/fields.py", line 66, in clean
value = self.widget.clean(value, row=data)
TypeError: clean() got an unexpected keyword argument 'row'

资源是这样构建的 正如我所说,我无法确定引发此错误的小部件是什么。这就是为什么我将所有 Class 粘贴在这里:(我知道太长了)

class retailResource(ModelResource):
    client = fields.Field(
        column_name='client',
        attribute='client',
        widget=widgets.ForeignKeyWidget(Client, 'name')
    )
    country = fields.Field(
        column_name='country',
        attribute='country',
        widget=widgets.ForeignKeyWidget(Country, 'name')
    )
    catalogue_type = fields.Field(
        column_name='catalogue_type',
        attribute='catalogue_type',
        widget=widgets.ForeignKeyWidget(CatalogueType, 'name')
    )
    currency_in_report = fields.Field(
        column_name='currency_in_report',
        attribute='currency_in_report',
        widget=widgets.ForeignKeyWidget(Currency, 'iso3_code')
    )
    start_date = fields.Field(
        attribute='start_date',
        widget=fields.Field(
            attribute='start_date',
            column_name='start_date',
            widget=widgets.DateWidget('%d/%m/%Y'))
    )
    end_date = fields.Field(
        attribute='end_date',
        widget=widgets.DateWidget('%d/%m/%Y')
    )

    class Meta():
        model = RetailPrice
        fields = ('id','client', 'country', 'catalogue_type','quality','gross_retail_price',
                  'currency_in_report','reported_as','start_date','end_date','comments',)
        export_order = ['id','client', 'country', 'catalogue_type','quality','gross_retail_price',
                  'currency_in_report','reported_as','start_date','end_date','comments']
        import_id_fields = ('id',)

管理员是这样的

class retailAdmin(ImportExportModelAdmin):
    resource_class = retailResource
  [...]

所以是的,问题是将 widget=widgets.DateWidget 用于日期字段。 我删除了那部分资源,就这样留下了。它起作用了。 所以,这个方法行得通。

class retailResource(ModelResource):
    client = fields.Field(
        column_name='client',
        attribute='client',
        widget=widgets.ForeignKeyWidget(Client, 'name')
    )
    country = fields.Field(
        column_name='country',
        attribute='country',
        widget=widgets.ForeignKeyWidget(Country, 'name')
    )
    catalogue_type = fields.Field(
        column_name='catalogue_type',
        attribute='catalogue_type',
        widget=widgets.ForeignKeyWidget(CatalogueType, 'name')
    )
    currency_in_report = fields.Field(
        column_name='currency_in_report',
        attribute='currency_in_report',
        widget=widgets.ForeignKeyWidget(Currency, 'iso3_code')
    )
 class Meta():
        model = RetailPrice
        fields = ('id','client', 'country', 'catalogue_type','quality','gross_retail_price',
                  'currency_in_report','reported_as','start_date','end_date','comments',)
        export_order = ['id','client', 'country', 'catalogue_type','quality','gross_retail_price',
                  'currency_in_report','reported_as','start_date','end_date','comments']
        import_id_fields = ('id',)

如果要覆盖行条目中使用的日期格式,则必须使用 DateWidget

它是可选的:如果你不使用 DateWidget 那么逻辑仍然会在幕后使用 DateWidget,但会依赖于你在 settings.DATE_INPUT_FORMATS 中声明的格式.我希望这就是当您删除 widget=DateTimeWidget()

时它起作用的原因

查看您的原始代码,注意 widget 的声明如何包含 Field 声明,这显然是不正确的。如果您更正此问题,我认为它会正常工作。

    start_date = fields.Field(
        attribute='start_date',
        widget=fields.Field(
            attribute='start_date',
            column_name='start_date',
            widget=widgets.DateWidget('%d/%m/%Y'))
    )