Widgets

class import_export.widgets.Widget

A Widget takes care of converting between import and export representations.

This is achieved by the two methods, clean() and render().

clean(value, row=None, *args, **kwargs)

Returns an appropriate Python object for an imported value.

For example, if you import a value from a spreadsheet, clean() handles conversion of this value into the corresponding Python object.

Numbers or dates can be cleaned to their respective data types and don’t have to be imported as Strings.

render(value, obj=None)

Returns an export representation of a Python value.

For example, if you have an object you want to export, render() takes care of converting the object’s field to a value that can be written to a spreadsheet.

class import_export.widgets.IntegerWidget

Widget for converting integer fields.

class import_export.widgets.DecimalWidget

Widget for converting decimal fields.

class import_export.widgets.CharWidget

Widget for converting text fields.

class import_export.widgets.BooleanWidget

Widget for converting boolean fields.

class import_export.widgets.DateWidget(format=None)

Widget for converting date fields.

Takes optional format parameter.

class import_export.widgets.TimeWidget(format=None)

Widget for converting time fields.

Takes optional format parameter.

class import_export.widgets.DateTimeWidget(format=None)

Widget for converting date fields.

Takes optional format parameter. If none is set, either settings.DATETIME_INPUT_FORMATS or "%Y-%m-%d %H:%M:%S" is used.

class import_export.widgets.ForeignKeyWidget(model, field=u'pk', *args, **kwargs)

Widget for a ForeignKey field which looks up a related model using “natural keys” in both export an import.

The lookup field defaults to using the primary key (pk) as lookup criterion but can be customised to use any field on the related model.

Unlike specifying a related field in your resource like so…

class Meta:
    fields = ('author__name',)

…using a ForeignKeyWidget has the advantage that it can not only be used for exporting, but also importing data with foreign key relationships.

Here’s an example on how to use ForeignKeyWidget to lookup related objects using Author.name instead of Author.pk:

class BookResource(resources.ModelResource):
    author = fields.Field(
        column_name='author',
        attribute='author',
        widget=ForeignKeyWidget(Author, 'name'))

    class Meta:
        fields = ('author',)
Parameters:
  • model – The Model the ForeignKey refers to (required).
  • field – A field on the related model used for looking up a particular object.
get_queryset(value, row, *args, **kwargs)

Returns a queryset of all objects for this Model.

Overwrite this method if you want to limit the pool of objects from which the related object is retrieved.

Parameters:
  • value – The field’s value in the datasource.
  • row – The datasource’s current row.

As an example; if you’d like to have ForeignKeyWidget look up a Person by their pre- and lastname column, you could subclass the widget like so:

class FullNameForeignKeyWidget(ForeignKeyWidget):
    def get_queryset(self, value, row):
        return self.model.objects.filter(
            first_name__iexact=row["first_name"],
            last_name__iexact=row["last_name"]
        )
class import_export.widgets.ManyToManyWidget(model, separator=None, field=None, *args, **kwargs)

Widget that converts between representations of a ManyToMany relationships as a list and an actual ManyToMany field.

Parameters:
  • model – The model the ManyToMany field refers to (required).
  • separator – Defaults to ','.
  • field – A field on the related model. Default is pk.