Ctrl + Alt + R,打开manage.py命令行,更快地执行startapp、migrate等命令
class Department(models.Model): """ 部门表 """ title = models.CharField(verbose_name='标题', max_length=32) class UserInfo(models.Model): """ 员工表 """ name = models.CharField(verbose_name='标题', max_length=32) password = models.CharField(verbose_name='密码', max_length=64) age = models.IntegerField(verbose_name='年龄') account = models.DecimalField(verbose_name='工资', max_digits=10, decimal_places=2, default=0) create_time = models.TimeField(verbose_name='入职时间') depart_id = models.ForeignKey(to='Department', to_field='id', on_delete=models.CASCADE) depart_id = models.ForeignKey(to='Department', to_field='id', blank=True,null=True, on_delete=models.SET_NULL) gender_choices=( (1, "男"), (2, "女"), ) gender =models.SmallIntegerField(verbose_name="性别",choices=gender_choices)
使用ForeignKey关联部门表ID
好处:节省服务器空间(大部分公司这么做,关系型数据库)
不使用ForeignKey,直接存值
好处:减少查询返回时间(互联网巨头才这么做,由于表被特别频繁地访问)
来源:BV1NL41157 武沛齐《2022 B站最详细django3教程(django从入门到实践)》P19-P22