【问题标题】:Django Import-export TypeError: clean() got an unexpected keyword argument 'row'Django Import-export TypeError: clean() got an unexpected keyword argument 'row'
【发布时间】:2020-11-12 16:00:49
【问题描述】:

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

这是我的 csv 示例:

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

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 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
  [...]

【问题讨论】:

  • 好吧,我已经能够在不使用小部件的情况下更新日期。所以现在我想知道? ¿ 我什么时候必须使用 DateWidget?

标签: django django-import-export


【解决方案1】:

当您想要覆盖行条目中使用的日期格式时,您必须使用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'))
    )

【讨论】:

    【解决方案2】:

    所以是的,问题在于将 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',)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2015-09-23
      相关资源
      最近更新 更多