Search code examples
djangodjango-import-export

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


When importing csv files it deliver the following error. I think this is related to the date widget?

This is an example of my 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'

Resource is build like this As I said, I can't be sure what is the widget that is raising this error. That's why I paste all the Class here: (I know is too long)

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',)

Admin is like this

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

Solution

  • You have to use DateWidget when you want to override the date format used in the row entry.

    It is optional: if you don't use DateWidget then the logic will still use DateWidget behind the scenes, but will rely on the format you have declared in settings.DATE_INPUT_FORMATS. I expect this is why it is working when you remove widget=DateTimeWidget()

    Looking at your original code, notice how the declaration for widget has a Field declaration, which is obviously incorrect. I think it would work ok if you corrected this.

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