1,创建项目:MROS
2, 在models.py文件中创建相关的表
from django.db import models # Create your models here. # 数据库表 # 用户表 class User(models.Model): name = models.CharField(max_length=32, verbose_name='用户名') pwd = models.CharField(max_length=32, verbose_name='密码') def __str__(self): return self.name # 只在admin中用,用于展示汉语表名称 class Meta: verbose_name = '用户表' # 单数 verbose_name_plural = verbose_name # 复数 # 会议室表 class Meeting(models.Model): title = models.CharField(max_length=32, verbose_name='会议室名称') def __str__(self): return self.title # 只在admin用,用于展示汉语表名称 class Meta: verbose_name = '会议室表' # 单数 verbose_name_plural = verbose_name # 复数 # 预定表 class Presettime(models.Model): reserve_data = models.DateTimeField(null=True, blank=True) # 预定时间 # 关联字段(ForeignKey:一对多),to:要关联表的表名,related_name:反向查找(本身表名复数) user = models.ForeignKey(to='User', related_name='preset_times') meeting = models.ForeignKey(to='Meeting', related_name='preset_times') # 时间段 time_choices = ( (1, '8:30'), (2, '9:00'), (3, '9:30'), (4, '10:00'), (5, '10:30'), (6, '11:00'), (7, '11:30'), (8, '12:00'), (9, '12:30'), (10, '13:00'), (11, '13:30'), (12, '14:00'), (13, '14:30'), (14, '15:00'), (15, '15:30'), (16, '16:00'), (17, '16:30'), (18, '17:00'), (19, '17:30'), (20, '18:00'), (21, '18:30'), (22, '19:00'), (23, '19:30'), (24, '20:00'), (25, '20:30') ) # 开始时间 start_time = models.SmallIntegerField(verbose_name='开始时间', blank=True, null=True) # 结束时间 end_time = models.SmallIntegerField(verbose_name='结束时间', blank=True, null=True) # 只在admin用,用于展示汉语表名称 class Meta: verbose_name = '预定时间表' # 单数 verbose_name_plural = verbose_name # 复数
3,文件迁移(生成表)
1,在Tools中点击Run Manage.py Task..... 2, 输入:makemigrations回车, 输入migrate回车。完后关闭 3,在Pycharm左边菜单栏中找到db.sqlite3拖到右边database中即可,就可看到已创建好的所有表