model层的创建 1.userModel的设计 (1)把新建的user继承已经存在的user类,使得更加方便; class UserProfile(AbstractUser): nick_name = models.CharField(max_length=50, verbose_name=u"昵称", default="") birthday = models.DateField(verbose_name=u"生日", null=True, blank=True) gender = models.CharField(max_length=6, choices=(("male", u"男"), ("female", "女")), default="female") address = models.CharField(max_length=100, default=u"") mobile = models.CharField(max_length=11, null=True, blank=True) image = models.ImageField(upload_to="image/%Y/%m", default=u"image/default.png", max_length=100) class Meta: verbose_name = "用户信息" verbose_name_plural = verbose_name def __unicode__(self): return self.username (2)在settings中注册信息,AUTH_USER_MODEL = "users.UserProfile",使得继承重载 (3)使用makemigration和migrate命令进行迁移 (4)加入邮箱验证码和首页轮播图的model信息 class EmailVerifyRecord(models.Model): code = models.CharField(max_length=20, verbose_name=u"验证码") email = models.EmailField(max_length=50, verbose_name=u"邮箱") send_type = models.CharField(verbose_name=u"验证码类型", choices=(("register",u"注册"),("forget",u"找回密码"), ("update_email",u"修改邮箱")), max_length=30) send_time = models.DateTimeField(verbose_name=u"发送时间", default=datetime.now) class Meta: verbose_name = u"邮箱验证码" verbose_name_plural = verbose_name def __unicode__(self): return '{0}({1})'.format(self.code, self.email) class Banner(models.Model): title = models.CharField(max_length=100, verbose_name=u"标题") image = models.ImageField(upload_to="banner/%Y/%m", verbose_name=u"轮播图", max_length=100) url = models.URLField(max_length=200, verbose_name=u"访问地址") index = models.IntegerField(default=100, verbose_name=u"顺序") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"轮播图" verbose_name_plural = verbose_name 2.courseModel的设计 (1)class Course(models.Model): name = models.CharField(max_length=50, verbose_name=u"课程名") desc = models.CharField(max_length=300, verbose_name=u"课程描述") detail = models.TextField(verbose_name=u"课程详情") degree = models.CharField(verbose_name=u"难度", choices=(("cj", "初级"), ("zj", "中级"), ("gj","高级")), max_length=2) learn_times = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)") students = models.IntegerField(default=0, verbose_name=u'学习人数') fav_nums = models.IntegerField(default=0, verbose_name=u'收藏人数') image = models.ImageField(upload_to="courses/%Y/%m", verbose_name=u"封面图", max_length=100) click_nums = models.IntegerField(default=0, verbose_name=u"点击数") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"课程" verbose_name_plural = verbose_name (2)django不存在一对多或者多对对于一的关系,只存在外键的映射; (3)外键的设置 class BannerCourse(Course): class Meta: verbose_name = "轮播课程" verbose_name_plural = verbose_name proxy = True class Lesson(models.Model): course = models.ForeignKey(Course, verbose_name=u"课程") name = models.CharField(max_length=100, verbose_name=u"章节名") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"章节" verbose_name_plural = verbose_name class Video(models.Model): lesson = models.ForeignKey(Lesson, verbose_name=u"章节") name = models.CharField(max_length=100, verbose_name=u"视频名") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"视频" verbose_name_plural = verbose_name class CourseResource(models.Model): course = models.ForeignKey(Course, verbose_name=u"课程") name = models.CharField(max_length=100, verbose_name=u"名称") download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name=u"资源文件", max_length=100) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"课程资源" verbose_name_plural = verbose_name 3.organizationModel的设计: class CityDict(models.Model): name = models.CharField(max_length=20, verbose_name=u"城市") desc = models.CharField(max_length=200, verbose_name=u"描述") add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u"城市" verbose_name_plural = verbose_name class CourseOrg(models.Model): name = models.CharField(max_length=50, verbose_name=u"机构名称") desc = models.TextField(verbose_name=u"机构描述") tag = models.CharField(default="全国知名", max_length=10, verbose_name=u"机构标签") category = models.CharField(default="pxjg", verbose_name=u"机构类别", max_length=20, choices=(("pxjg", "培训机构"), ("gr", "个人"), ("gx", "高校"))) click_nums = models.IntegerField(default=0, verbose_name=u"点击数") fav_nums = models.IntegerField(default=0, verbose_name=u"收藏数") image = models.ImageField(upload_to="org/%Y/%m", verbose_name=u"logo", max_length=100) address = models.CharField(max_length=150, verbose_name=u"机构地址") students = models.IntegerField(default=0, verbose_name=u"学习人数") course_nums = models.IntegerField(default=0, verbose_name=u"课程数") add_time = models.DateTimeField(default=datetime.now) city = models.ForeignKey(CityDict, verbose_name=u"所在城市") class Meta: verbose_name = u"课程机构" verbose_name_plural = verbose_name class Teacher(models.Model): org = models.ForeignKey(CourseOrg, verbose_name=u"所属机构") name = models.CharField(max_length=50, verbose_name=u"教师名") work_years = models.IntegerField(default=0, verbose_name=u"工作年限") work_company = models.CharField(max_length=50, verbose_name=u"就职公司") work_position = models.CharField(max_length=50, verbose_name=u"公司职位") points = models.CharField(max_length=50, verbose_name=u"教学特点") click_nums = models.IntegerField(default=0, verbose_name=u"点击数") fav_nums = models.IntegerField(default=0, verbose_name=u"收藏数") age = models.IntegerField(default=18, verbose_name=u"年龄") image = models.ImageField(default='', upload_to="teacher/%Y/%m", verbose_name=u"头像", max_length=100) add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u"教师" verbose_name_plural = verbose_name 4.operationModel的设计 class UserAsk(models.Model): name = models.CharField(max_length=20, verbose_name=u"姓名") mobile = models.CharField(max_length=11, verbose_name=u"手机") course_name = models.CharField(max_length=50, verbose_name=u"课程名") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"用户咨询" verbose_name_plural = verbose_name class CourseComments(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用户") course = models.ForeignKey(Course, verbose_name=u"课程") comments = models.CharField(max_length=200, verbose_name=u"评论") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"课程评论" verbose_name_plural = verbose_name class UserFavorite(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用户") fav_id = models.IntegerField(default=0, verbose_name=u"数据id") fav_type = models.IntegerField(choices=((1, "课程"), (2, "课程机构"),(3, "讲师")), default=1, verbose_name=u"收藏类型") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"用户收藏" verbose_name_plural = verbose_name class UserMessage(models.Model): user = models.IntegerField(default=0, verbose_name=u"接收用户") message = models.CharField(max_length=500, verbose_name=u"消息内容") has_read = models.BooleanField(default=False, verbose_name=u"是否已读") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"用户消息" verbose_name_plural = verbose_name class UserCourse(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用户") course = models.ForeignKey(Course, verbose_name=u"课程") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"用户课程" verbose_name_plural = verbose_name 5.根据上面的model生成相应的表结构 1)直接使用makemigration命令 2)新建包-apps 3)把其余包拖入apps 4)把apps文件夹makesource 5)在settings中,把apps文件夹的路径加入进来,sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))
【django+xadmin实战】0129笔记——models的设计
最新推荐文章于 2020-01-05 17:35:07 发布