Django模型CharField字段类型定义更为优雅的Choice

官方推荐的一种写法:

class SexType(objects):
    MALE = 'M'
    FEMALE = 'F'
    UNKNOWN = 'N/A'

    CHOICES = (
        (MALE, '男'),
        (FEMALE, '女'),
        (UNKNOWN, u'未知'),
    )


class User(models.Model):
    username = models.CharField(max_length=20, unique=True, verbose_name='名称')
    sex = models.CharField(max_length=3, default=SexType.UNKNOWN, choices=SexType.CHOICES, verbose_name='性别')

效果:

  1. 数据库存放的值为 ‘M'  'F'  'N/A'
  2. 业务逻辑中方便判断:User.objects.filter(sex=SexType.MALE)
  3. 序列化器方便返回字段的描述信息(label值):
class UserSerializer(serializers.ModelSerializer):
    sex_t = serializers.CharField(source='get_sex_display', read_only=True, required=False)
    #source='get_sex_display' 固定写法get_xxx_display
    class Meta:
        model = User
        fields = ('id', 'name', 'sex', 'sex_t')

序列化结果:
[
    {"id": 1,
     "name": "小明",
     "sex": "M",
     "sex_t": "男"
    },
    {"id": 2,
     "name": "小王",
     "sex": "F",
     "sex_t": "女"
    }
]

 

你可能感兴趣的:(Django)