choice参数
用户性别
用户学历
用户工作状态
客户来源
...
models.py 模型层
from django.db import models
class Userinfo(models.Model):
gender_choices = (
(1,'male'),
(2,'female'),
(3,'others')
)
# 将变量存在choice
gender = models.IntegerField(choices=gender_choices)
tests.py 用来测试的文件
固定句式 数据对象.get_字段名_display()
当没有对应关系的时候 该句式获取到的还是数字
from django.test import TestCase # 从test里面导入TestCase
# 从manage.py里面复制三个东西 配置默认环境变量
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day62.settings")
# 配置
import django
django.setup()
from app01 import models
user_obj = models.Userinfo.objects.filter(pk=4).first()
print(user_obj.username)
print(user_obj.gender)
# 针对choices字段 如果你想要获取数字所对应的中文 你不能直接点字段
print(user_obj.get_gender_display())
record_choices = (('checked', "已签到"),
('vacate', "请假"),
('late', "迟到"),
('noshow', "缺勤"),
('leave_early', "早退"),
)
record = models.CharField("上课纪录", choices=record_choices, default="checked",
score_choices = ((100, 'A+'),
(90, 'A'),
(85, 'B+'),
(80, 'B'),
(70, 'B-'),
(60, 'C+'),
(50, 'C'),
(40, 'C-'),
(0, ' D'),
(-1, 'N/A'),
(-100, 'COPY'),
(-1000, 'FAIL'),
)
score = models.IntegerField("本节成绩", choices=score_choices, default=-1)