choices参数(数据库字段设计常见)
""" 用户表 性别 学历 工作经验 是否结婚 是否生子 客户来源 ... 针对某个可以列举完全的可能性字段,我们应该如何存储 只要某个字段的可能性是可以列举完全的,那么一般情况下都会采用choices参数 """ class User(models.Model): username = models.CharField(max_length=32) age = models.IntegerField() # 性别 gender_choices = ( (1,'男'), (2,'女'), (3,'其他'), ) gender = models.IntegerField(choices=gender_choices) score_choices = ( ('A','优秀'), ('B','良好'), ('C','及格'), ('D','不合格'), ) # 保证字段类型跟列举出来的元祖第一个数据类型一致即可 score = models.CharField(choices=score_choices,null=True) """ 该gender字段存的还是数字 但是如果存的数字在上面元祖列举的范围之内 那么可以非常轻松的获取到数字对应的真正的内容 1.gender字段存的数字不在上述元祖列举的范围内容 2.如果在 如何获取对应的中文信息 """ from app01 import models # models.User.objects.create(username='jason',age=18,gender=1) # models.User.objects.create(username='egon',age=85,gender=2) # models.User.objects.create(username='tank',age=40,gender=3) # 存的时候 没有列举出来的数字也能存(范围还是按照字段类型决定) # models.User.objects.create(username='tony',age=45,gender=4) # 取 # user_obj = models.User.objects.filter(pk=1).first() # print(user_obj.gender) # 只要是choices参数的字段 如果你想要获取对应信息 固定写法 get_字段名_display() # print(user_obj.get_gender_display()) user_obj = models.User.objects.filter(pk=4).first() # 如果没有对应关系 那么字段是什么还是展示什么 print(user_obj.get_gender_display()) # 4
实际项目案例
1 # CRM相关内部表 2 class School(models.Model): 3 """ 4 校区表 5 如: 6 北京沙河校区 7 上海校区 8 9 """ 10 title = models.CharField(verbose_name='校区名称', max_length=32) 11 12 def __str__(self): 13 return self.title 14 15 class Course(models.Model): 16 """ 17 课程表 18 如: 19 Linux基础 20 Linux架构师 21 Python自动化开发精英班 22 Python自动化开发架构师班 23 Python基础班 24 go基础班 25 """ 26 name = models.CharField(verbose_name='课程名称', max_length=32) 27 28 def __str__(self): 29 return self.name 30 31 class Department(models.Model): 32 """ 33 部门表 34 市场部 1000 35 销售 1001 36 37 """ 38 title = models.CharField(verbose_name='部门名称', max_length=16) 39 code = models.IntegerField(verbose_name='部门编号', unique=True, null=False) 40 41 def __str__(self): 42 return self.title 43 44 class UserInfo(models.Model): 45 """ 46 员工表 47 """ 48 49 name = models.CharField(verbose_name='员工姓名', max_length=16) 50 email = models.EmailField(verbose_name='邮箱', max_length=64) 51 depart = models.ForeignKey(verbose_name='部门', to="Department",to_field="code") 52 user=models.OneToOneField("User",default=1) 53 def __str__(self): 54 return self.name 55 56 class ClassList(models.Model): 57 """ 58 班级表 59 如: 60 Python全栈 面授班 5期 10000 2017-11-11 2018-5-11 61 """ 62 school = models.ForeignKey(verbose_name='校区', to='School') 63 course = models.ForeignKey(verbose_name='课程名称', to='Course') 64 semester = models.IntegerField(verbose_name="班级(期)") 65 66 67 price = models.IntegerField(verbose_name="学费") 68 start_date = models.DateField(verbose_name="开班日期") 69 graduate_date = models.DateField(verbose_name="结业日期", null=True, blank=True) 70 memo = models.CharField(verbose_name='说明', max_length=256, blank=True, null=True, ) 71 72 teachers = models.ManyToManyField(verbose_name='任课老师', to='UserInfo',limit_choices_to={'depart':1002}) 73 tutor = models.ForeignKey(verbose_name='班主任', to='UserInfo',related_name="class_list",limit_choices_to={'depart':1006}) 74 75 76 def __str__(self): 77 return "{0}({1}期)".format(self.course.name, self.semester) 78 79 80 class Customer(models.Model): 81 """ 82 客户表 83 """ 84 qq = models.CharField(verbose_name='qq', max_length=64, unique=True, help_text='QQ号必须唯一') 85 86 name = models.CharField(verbose_name='学生姓名', max_length=16) 87 gender_choices = ((1, '男'), (2, '女')) 88 gender = models.SmallIntegerField(verbose_name='性别', choices=gender_choices) 89 90 education_choices = ( 91 (1, '重点大学'), 92 (2, '普通本科'), 93 (3, '独立院校'), 94 (4, '民办本科'), 95 (5, '大专'), 96 (6, '民办专科'), 97 (7, '高中'), 98 (8, '其他') 99 ) 100 education = models.IntegerField(verbose_name='学历', choices=education_choices, blank=True, null=True, ) 101 graduation_school = models.CharField(verbose_name='毕业学校', max_length=64, blank=True, null=True) 102 major = models.CharField(verbose_name='所学专业', max_length=64, blank=True, null=True) 103 104 experience_choices = [ 105 (1, '在校生'), 106 (2, '应届毕业'), 107 (3, '半年以内'), 108 (4, '半年至一年'), 109 (5, '一年至三年'), 110 (6, '三年至五年'), 111 (7, '五年以上'), 112 ] 113 experience = models.IntegerField(verbose_name='工作经验', blank=True, null=True, choices=experience_choices) 114 work_status_choices = [ 115 (1, '在职'), 116 (2, '无业') 117 ] 118 work_status = models.IntegerField(verbose_name="职业状态", choices=work_status_choices, default=1, blank=True, 119 null=True) 120 company = models.CharField(verbose_name="目前就职公司", max_length=64, blank=True, null=True) 121 salary = models.CharField(verbose_name="当前薪资", max_length=64, blank=True, null=True) 122 123 source_choices = [ 124 (1, "qq群"), 125 (2, "内部转介绍"), 126 (3, "官方网站"), 127 (4, "百度推广"), 128 (5, "360推广"), 129 (6, "搜狗推广"), 130 (7, "腾讯课堂"), 131 (8, "广点通"), 132 (9, "高校宣讲"), 133 (10, "渠道代理"), 134 (11, "51cto"), 135 (12, "智汇推"), 136 (13, "网盟"), 137 (14, "DSP"), 138 (15, "SEO"), 139 (16, "其它"), 140 ] 141 source = models.SmallIntegerField('客户来源', choices=source_choices, default=1) 142 referral_from = models.ForeignKey( 143 'self', 144 blank=True, 145 null=True, 146 verbose_name="转介绍自学员", 147 help_text="若此客户是转介绍自内部学员,请在此处选择内部学员姓名", 148 related_name="internal_referral" 149 ) 150 course = models.ManyToManyField(verbose_name="咨询课程", to="Course") 151 152 status_choices = [ 153 (1, "已报名"), 154 (2, "未报名") 155 ] 156 status = models.IntegerField( 157 verbose_name="状态", 158 choices=status_choices, 159 default=2, 160 help_text=u"选择客户此时的状态" 161 ) 162 163 consultant = models.ForeignKey(verbose_name="课程顾问", to='UserInfo', related_name='consultanter',limit_choices_to={'depart':1001}) 164 165 date = models.DateField(verbose_name="咨询日期", auto_now_add=True) 166 recv_date = models.DateField(verbose_name="当前课程顾问的接单日期", null=True) 167 last_consult_date = models.DateField(verbose_name="最后跟进日期", ) 168 169 def __str__(self): 170 return self.name 171 172 class ConsultRecord(models.Model): 173 """ 174 客户跟进记录 175 """ 176 customer = models.ForeignKey(verbose_name="所咨询客户", to='Customer') 177 consultant = models.ForeignKey(verbose_name="跟踪人", to='UserInfo',limit_choices_to={'depart':1001}) 178 date = models.DateField(verbose_name="跟进日期", auto_now_add=True) 179 note = models.TextField(verbose_name="跟进内容...") 180 181 def __str__(self): 182 return self.customer.name + ":" + self.consultant.name 183 184 class Student(models.Model): 185 """ 186 学生表(已报名) 187 """ 188 customer = models.OneToOneField(verbose_name='客户信息', to='Customer') 189 class_list = models.ManyToManyField(verbose_name="已报班级", to='ClassList', blank=True) 190 191 emergency_contract = models.CharField(max_length=32, blank=True, null=True, verbose_name='紧急联系人') 192 company = models.CharField(verbose_name='公司', max_length=128, blank=True, null=True) 193 location = models.CharField(max_length=64, verbose_name='所在区域', blank=True, null=True) 194 position = models.CharField(verbose_name='岗位', max_length=64, blank=True, null=True) 195 salary = models.IntegerField(verbose_name='薪资', blank=True, null=True) 196 welfare = models.CharField(verbose_name='福利', max_length=256, blank=True, null=True) 197 date = models.DateField(verbose_name='入职时间', help_text='格式yyyy-mm-dd', blank=True, null=True) 198 memo = models.CharField(verbose_name='备注', max_length=256, blank=True, null=True) 199 200 def __str__(self): 201 return self.customer.name 202 203 class ClassStudyRecord(models.Model): 204 """ 205 上课记录表 (班级记录) 206 """ 207 class_obj = models.ForeignKey(verbose_name="班级", to="ClassList") 208 day_num = models.IntegerField(verbose_name="节次", help_text=u"此处填写第几节课或第几天课程...,必须为数字") 209 teacher = models.ForeignKey(verbose_name="讲师", to='UserInfo',limit_choices_to={'depart':1002}) 210 date = models.DateField(verbose_name="上课日期", auto_now_add=True) 211 212 course_title = models.CharField(verbose_name='本节课程标题', max_length=64, blank=True, null=True) 213 course_memo = models.TextField(verbose_name='本节课程内容概要', blank=True, null=True) 214 has_homework = models.BooleanField(default=True, verbose_name="本节有作业") 215 homework_title = models.CharField(verbose_name='本节作业标题', max_length=64, blank=True, null=True) 216 homework_memo = models.TextField(verbose_name='作业描述', max_length=500, blank=True, null=True) 217 exam = models.TextField(verbose_name='踩分点', max_length=300, blank=True, null=True) 218 219 def __str__(self): 220 return "{0} day{1}".format(self.class_obj, self.day_num) 221 222 class StudentStudyRecord(models.Model): 223 ''' 224 学生学习记录 225 ''' 226 classstudyrecord = models.ForeignKey(verbose_name="第几天课程", to="ClassStudyRecord") 227 student = models.ForeignKey(verbose_name="学员", to='Student') 228 229 230 231 232 233 234 235 record_choices = (('checked', "已签到"), 236 ('vacate', "请假"), 237 ('late', "迟到"), 238 ('noshow', "缺勤"), 239 ('leave_early', "早退"), 240 ) 241 record = models.CharField("上课纪录", choices=record_choices, default="checked", max_length=64) 242 score_choices = ((100, 'A+'), 243 (90, 'A'), 244 (85, 'B+'), 245 (80, 'B'), 246 (70, 'B-'), 247 (60, 'C+'), 248 (50, 'C'), 249 (40, 'C-'), 250 (0, ' D'), 251 (-1, 'N/A'), 252 (-100, 'COPY'), 253 (-1000, 'FAIL'), 254 ) 255 score = models.IntegerField("本节成绩", choices=score_choices, default=-1) 256 homework_note = models.CharField(verbose_name='作业评语', max_length=255, blank=True, null=True) 257 note = models.CharField(verbose_name="备注", max_length=255, blank=True, null=True) 258 259 homework = models.FileField(verbose_name='作业文件', blank=True, null=True, default=None) 260 stu_memo = models.TextField(verbose_name='学员备注', blank=True, null=True) 261 date = models.DateTimeField(verbose_name='提交作业日期', auto_now_add=True) 262 263 def __str__(self): 264 return "{0}-{1}".format(self.classstudyrecord, self.student) 265 266 """ 267 chocies参数使用场景是非常广泛的 268 """View Code MTV与MVC模型
# MTV:Django号称是MTV模型 M:models T:templates V:views # MVC:其实django本质也是MVC M:models V:views C:controller # vue框架:MVVM模型
多对多三种创建方式
# 全自动:利用orm自动帮我们创建第三张关系表 class Book(models.Model): name = models.CharField(max_length=32) authors = models.ManyToManyField(to='Author') class Author(models.Model): name = models.CharField(max_length=32) """ 优点:代码不需要你写 非常的方便 还支持orm提供操作第三张关系表的方法... 不足之处:第三张关系表的扩展性极差(没有办法额外添加字段...) """ # 纯手动 class Book(models.Model): name = models.CharField(max_length=32) class Author(models.Model): name = models.CharField(max_length=32) class Book2Author(models.Model): book_id = models.ForeignKey(to='Book') author_id = models.ForeignKey(to='Author') ''' 优点:第三张表完全取决于你自己进行额外的扩展 不足之处:需要写的代码较多,不能够再使用orm提供的简单的方法 不建议你用该方式 ''' # 半自动 class Book(models.Model): name = models.CharField(max_length=32) authors = models.ManyToManyField(to='Author', through='Book2Author', through_fields=('book','author') ) class Author(models.Model): name = models.CharField(max_length=32) # books = models.ManyToManyField(to='Book', # through='Book2Author', # through_fields=('author','book') # ) class Book2Author(models.Model): book = models.ForeignKey(to='Book') author = models.ForeignKey(to='Author') """ through_fields字段先后顺序 判断的本质: 通过第三张表查询对应的表 需要用到哪个字段就把哪个字段放前面 你也可以简化判断 当前表是谁 就把对应的关联字段放前面 半自动:可以使用orm的正反向查询 但是没法使用add,set,remove,clear这四个方法 """
标签:name,models,max,MTV,choices,MCV,True,verbose From: https://www.cnblogs.com/shuai61457/p/17501102.html