简体   繁体   中英

How to seperate import and export fields in Django-Import-Export

I am using Django-Import-Export and have a model-fields like below

id, date, val1, val2, val3

what i want is to import by only 2 fields (date and val1)

and i want to export 4 fields (date val1 val2 val3)

if i do like below i can see import fields changing but in export only 2 fields are coming

class MyResource(ExportImportObjectHere):
  
    class Meta:
        model = ModelName
        skip_unchanged = True
        fields = ('date', 'val1')
        

You can override get_import_fields() and get_export_fields() :

class MyResource(ExportImportObjectHere):
    get_import_fields(self):
        fields = super().get_import_fields()
        return [f for f in fields if f.attribute in self._meta.import_fields]

    def get_export_fields(self):
        fields = super().get_export_fields()
        return [f for f in fields if f.attribute in self._meta.export_fields]

    class Meta:
        model = ModelName
        skip_unchanged = True
        fields = ('date', 'val1')

        # these lists declare the attribute name of the model
        import_fields = ('date', 'val1')
        export_fields = ('date', 'val1', 'val2', 'val3')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM