import m2m relation in django-import-export

前端 未结 1 1781
广开言路
广开言路 2021-02-04 07:02

use application django-import-export. Here is an example configuration for one of the models:

class ImportExportAdsTypeResource(resources.ModelResource):
    cla         


        
相关标签:
1条回答
  • 2021-02-04 07:55

    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

    0 讨论(0)
提交回复
热议问题