import os if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dd85.settings')
#这句直接去wsgi.py中拷贝过来即可 import django django.setup() from app03 import models student_list=models.Student.objects.all() for student in student_list: print(student.sex) #只要写了choice,配好了关系,该对象就会有个get_字段名_display()的方法 print(student.get_sex_display())
#本身choice只会显示数字 display 帮我们显示数字所对应的值
models.py:
class Student(models.Model): choice_sex=((1,'男'),(1,'女'),(3,'人妖')) name=models.CharField(max_length=32) sex=models.SmallIntegerField(choices=choice_sex,default=1) # hobby=models.CharField(choices=[('1','篮球'),('2','足球')]) age=models.IntegerField()
serializer.py:
from app03 import models from rest_framework import serializers class StudentSerializer(serializers.ModelSerializer): sex=serializers.CharField(source='get_sex_display') class Meta: model =models.Student fields = '__all__'
标签:脚本,__,models,sex,导入,student,import,django From: https://www.cnblogs.com/97zs/p/17958343