[Fixed]-Import m2m relation in django-import-export

11👍

django-import-export has it’s own widgets to handle models relationships:

    from import_export import fields, resources
    from import_export.widgets import ManyToManyWidget

    class ImportExportAdsTypeResource(resources.ModelResource):
        categories = fields.Field(widget=ManyToManyWidget(Category))
        sites = fields.Field(widget=ManyToManyWidget(Site))

        class Meta:
            model = AdType
            import_id_fields = ('name',)
            fields = ['name', 'active', 'position', 'categories', 'sites']

Check here for other widgets: django-import-export Widgets

👤Dhia

Leave a comment