I have a short question about django-import-export. In my model I have choice list:
STATE_CHOICES = ((NEW_STATE, u'New'),
(DELIVERED_STATE, u'Delivered'),
(LOST_STATE, u'Lost'),
And method that handles mapping choices for names
@staticmethod
def get_status_name_by_status(status):
return next((s[1] for s in MyModel.STATE_CHOICES if s[0] == status), 'Uknown')
I want to import/export some data
class MyModelResource(resources.ModelResource):
status = fields.Field(column_name='status', attribute='order',
widget=ForeignKeyWidget(Order, 'status'))
I want to use my get_status_name_by_status method so the choices will be converted to names. But there is no possibility to use method here, only fields are allowed. Any tip how this can be done ?
A hack method to realize this quickly, just configure
_choice_fields
to your choices fields, that's it.Not all data can be easily extracted from an object/model attribute. In order to turn complicated data model into a (generally simpler) processed data structure, dehydrate_fieldname method should be defined:
You can use 'get_FOO_display' to achieve this in the Django Admin:
if you want import and export having different field inside Django import-export.