Django import-export choices field

前端 未结 2 1237
栀梦
栀梦 2021-01-20 15:06

I have a model with choices list (models.py):

class Product(models.Model):
    ...
    UNITS_L = 1
    UNITS_SL = 2
    UNITS_XL = 3
    PRODUCT         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 15:30

    Another solution:

    class BaseModelResource(resources.ModelResource):
    
        def export_field(self, field, obj):
            field_name = self.get_field_name(field)
            func_name = 'get_{}_display'.format(field_name)
            if hasattr(obj, func_name):
                return getattr(obj, func_name)
            return super().export_field(field, obj)
    
    
    class ProductReportResource(BaseModelResource):
        ...
    

提交回复
热议问题