zoukankan      html  css  js  c++  java
  • 3、Django实战第3天:数据建模

    users modesl.py设计

    UserProfile 用户信息
    EmailVerifyRecord 验证码
    Banner 轮播图

    之前我们生成了Django默认的数据表,里面有张表示auth-user

    mysql> desc auth_user;
    +--------------+--------------+------+-----+---------+----------------+
    | Field        | Type         | Null | Key | Default | Extra          |
    +--------------+--------------+------+-----+---------+----------------+
    | id           | int(11)      | NO   | PRI | NULL    | auto_increment |
    | password     | varchar(128) | NO   |     | NULL    |                |
    | last_login   | datetime(6)  | YES  |     | NULL    |                |
    | is_superuser | tinyint(1)   | NO   |     | NULL    |                |
    | username     | varchar(30)  | NO   | UNI | NULL    |                |
    | first_name   | varchar(30)  | NO   |     | NULL    |                |
    | last_name    | varchar(30)  | NO   |     | NULL    |                |
    | email        | varchar(254) | NO   |     | NULL    |                |
    | is_staff     | tinyint(1)   | NO   |     | NULL    |                |
    | is_active    | tinyint(1)   | NO   |     | NULL    |                |
    | date_joined  | datetime(6)  | NO   |     | NULL    |                |
    +--------------+--------------+------+-----+---------+----------------+
    

    根据项目的需求,这些字段无法满足,因此我们还需要添加一些字段;编辑apps.users.models.py 新建一张表继承auth-user表

    from django.db import models
    from django.contrib.auth.models import AbstractUser  #导入auth-user表模块
    
    
    class UserProfile(AbstractUser):
        nick_name = models.CharField(max_length=50, verbose_name='昵称', default='')
        birthday = models.DateField(verbose_name='生日', null=True, blank=True)
        gender = models.CharField(max_length=6, choices=(('male', '男'),('female', '女')), default='female')
        address = models.CharField(max_length=100, default='')
        mobile = models.CharField(max_length=11, null=True, blank=True)
        image = models.ImageField(upload_to='image/%Y/%m', default='image/default.png', max_length=100)
    
        class Meta:
            verbose_name = '用户信息'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.username

    因为上面我们使用到了imageField字段类型,但是imageField字段依赖于Pillow

    (mxonline) C:Usersuser>pip install pillow
    

    修改settings.py,设置用户认证模型为我们新建的UserProfile

    AUTH_USER_MODEL = 'users.UserProfile'    #MIDDLEWARE_CLASSES行上面添加此行
    

    同步数据表到数据库

    manage.py@mxonline > makemigrations users
    manage.py@mxonline > migrate users    #中途需要确认,输入yes
    

    进入数据库查看表users_userprofile

    mysql> desc users_userprofile ;
    +--------------+--------------+------+-----+---------+----------------+
    | Field        | Type         | Null | Key | Default | Extra          |
    +--------------+--------------+------+-----+---------+----------------+
    | id           | int(11)      | NO   | PRI | NULL    | auto_increment |
    | password     | varchar(128) | NO   |     | NULL    |                |
    | last_login   | datetime(6)  | YES  |     | NULL    |                |
    | is_superuser | tinyint(1)   | NO   |     | NULL    |                |
    | username     | varchar(30)  | NO   | UNI | NULL    |                |
    | first_name   | varchar(30)  | NO   |     | NULL    |                |
    | last_name    | varchar(30)  | NO   |     | NULL    |                |
    | email        | varchar(254) | NO   |     | NULL    |                |
    | is_staff     | tinyint(1)   | NO   |     | NULL    |                |
    | is_active    | tinyint(1)   | NO   |     | NULL    |                |
    | date_joined  | datetime(6)  | NO   |     | NULL    |                |
    | nick_name    | varchar(50)  | NO   |     | NULL    |                |
    | birthday     | date         | YES  |     | NULL    |                |
    | gender       | varchar(6)   | NO   |     | NULL    |                |
    | address      | varchar(100) | NO   |     | NULL    |                |
    | mobile       | varchar(11)  | YES  |     | NULL    |                |
    | image        | varchar(100) | NO   |     | NULL    |                |
    +--------------+--------------+------+-----+---------+----------------+
    

    apps.users.models.py再加上两张表

    from django.db import models
    from django.contrib.auth.models import AbstractUser  # 导入auth-user表模块
    from datetime import datetime
    
    
    class UserProfile(AbstractUser):
        nick_name = models.CharField(max_length=50, verbose_name='昵称', default='')
        birthday = models.DateField(verbose_name='生日', null=True, blank=True)
        gender = models.CharField(max_length=6, choices=(('male', '男'), ('female', '女')), default='female')
        address = models.CharField(max_length=100, default='')
        mobile = models.CharField(max_length=11, null=True, blank=True)
        image = models.ImageField(upload_to='image/%Y/%m', default='image/default.png', max_length=100)
    
        class Meta:
            verbose_name = '用户信息'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.username
    
    
    class EmailVerifyRecord(models.Model):
        code = models.CharField(max_length=20, verbose_name='验证码')
        email = models.EmailField(max_length=50, verbose_name='邮箱')
        send_type = models.CharField(choices=(('register', '注册'), ('forget', '找回密码'),('hmail', '修改邮箱')), max_length=10, verbose_name='发送类型')
        send_time = models.DateField(default=datetime.now, verbose_name='发送时间')
    
        class Meta:
            verbose_name = '邮箱验证码'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return '{0}({1})'.format(self.code, self.email)
    
    
    
    class Banner(models.Model):
        title = models.CharField(max_length=100, verbose_name='标题')
        image = models.ImageField(upload_to='banner/%Y/%m', verbose_name='轮播图', max_length=100)
        url = models.URLField(max_length=200, verbose_name='访问地址')
        index = models.IntegerField(default=100, verbose_name='顺序')
        add_time = models.DateField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '轮播图'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.title
    

      

    最后执行,同步命令同步到数据库

    manage.py@mxonline > makemigrations users
    manage.py@mxonline > migrate users 
    

    organization modesl.py设计

    CourseOrg 课程机构
    Teacher 教师
    CityDict 城市

    编辑apps.organization.models.py

    from django.db import models
    from datetime import datetime
    
    
    class CityDict(models.Model):
        name = models.CharField(max_length=20, verbose_name='城市名')
        desc = models.CharField(max_length=200, verbose_name='描述')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '城市'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class CourseOrg(models.Model):
        name = models.CharField(max_length=50, verbose_name='机构名称')
        desc = models.TextField(verbose_name='机构描述')
        category = models.CharField(max_length=20, default='jg', choices=(('jg', '机构'), ('gr', '个人'), ('gx', '高校')),
                                    verbose_name='机构类别')
        click_nums = models.IntegerField(default=0, verbose_name='点击数')
        fav_nums = models.IntegerField(default=0, verbose_name='收藏数')
        image = models.ImageField(upload_to='organization/%Y/%m', verbose_name='封面图', max_length=100)
        address = models.CharField(max_length=150, verbose_name='机构地址', blank=True)
        city = models.ForeignKey(CityDict, verbose_name='所在城市')
        students = models.IntegerField(default=0, verbose_name='学习人数')
        course_nums = models.IntegerField(default=0, verbose_name='课程数')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '课程机构'
            verbose_name_plural = verbose_name
    
        def get_teracher_nums(self):
            """获取教师数"""
            return self.teacher_set.count()
    
        def __str__(self):
            return self.name
    
    
    class Teacher(models.Model):
        org = models.ForeignKey(CourseOrg, verbose_name='所属机构')
        name = models.CharField(max_length=50, verbose_name='教师名')
        age = models.ImageField(default=18, verbose_name='年龄')
        image = models.ImageField(upload_to='teacher/%Y/%m', verbose_name='头像', max_length=100)
        work_years = models.IntegerField(default=0, verbose_name='工作年限')
        work_company = models.CharField(max_length=50, verbose_name='就职公司')
        work_position = models.CharField(max_length=50, verbose_name='公司职位')
        points = models.CharField(max_length=50, verbose_name='教学特点')
        click_nums = models.IntegerField(default=0, verbose_name='点击数')
        fav_nums = models.IntegerField(default=0, verbose_name='收藏数')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '教师'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name

    courses models.py设计

    Course 课程信息
    Lesson 章节信息
    Vdeo 视频
    CourseResource 课程资源

    编辑apps.courses.models.py

    from django.db import models
    from datetime import datetime
    from organization.models import CourseOrg
    
    
    class Course(models.Model):
        name = models.CharField(max_length=50, verbose_name='课程名')
        course_org = models.ForeignKey(CourseOrg, verbose_name='课程机构')
        desc = models.CharField(max_length=300, verbose_name='课程描述')
        details = models.TextField(verbose_name='课程详情')
        degree = models.CharField(verbose_name='课程难度', choices=(('cj', '初级'), ('zj', '中级'), ('gj', '高级')), max_length=2)
        learn_times = models.IntegerField(default=0, verbose_name='学习时长(分钟)')
        students = models.IntegerField(default=0, verbose_name='学习人数')
        fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
        image = models.ImageField(upload_to='courses/%Y/%m', verbose_name='封面图', max_length=100)
        click_nums = models.IntegerField(default=0, verbose_name='点击数')
        category = models.CharField(max_length=20, default='', verbose_name=u'课程类别')
        tag = models.CharField(max_length=10, default='', verbose_name=u'课程标签')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
        class Meta:
            verbose_name = '课程'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class Lesson(models.Model):
        course = models.ForeignKey(Course, verbose_name='课程')
        name = models.CharField(max_length=100, verbose_name='章节名')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '章节'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class Video(models.Model):
        lesson = models.ForeignKey(Lesson, verbose_name='章节')
        name = models.CharField(max_length=100, verbose_name='视频名')
        url = models.URLField(max_length=150, verbose_name='视频地址', null=True, blank=True)
        learn_times = models.IntegerField(default=0, verbose_name='学习时长(分钟数)')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '视频'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class CourseResource(models.Model):
        course = models.ForeignKey(Course, verbose_name='课程')
        name = models.CharField(max_length=100, verbose_name='名称')
        download = models.FileField(upload_to='course/resource/%Y/%m', verbose_name='资源文件', max_length=100)
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '课程资源'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name

    operation models.py设计

    UserAsk 用户咨询
    CourseComments 用户评论
    UserFavorite 用户收藏
    UserMessage   用户信息
    UserCourse 用户学习的课程

    编辑apps.operation.models.py

    from django.db import models
    from datetime import datetime
    from users.models import UserProfile
    from courses.models import Course
    
    
    class UserAsk(models.Model):
        name = models.CharField(max_length=20, verbose_name='姓名')
        mobile = models.CharField(max_length=11, verbose_name='手机')
        course_name = models.CharField(max_length=50, verbose_name='课程名')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '用户咨询'
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.name
    
    
    class CourseComments(models.Model):
        user = models.ForeignKey(UserProfile, verbose_name='用户')
        course = models.ForeignKey(Course, verbose_name='课程')
        comments = models.CharField(max_length=200, verbose_name='评论')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '课程评论'
            verbose_name_plural = verbose_name
    
    
    class UserFavorite(models.Model):
        user = models.ForeignKey(UserProfile, verbose_name='用户')
        fav_id = models.IntegerField(default=0, verbose_name='数据id')
        fav_type = models.IntegerField(choices=((1, '课程'),(2, '机构'),(3, '讲师')), default=1, verbose_name='收藏类型')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '用户收藏'
            verbose_name_plural = verbose_name
    
    
    
    
    class UserMessage(models.Model):
        user = models.IntegerField(default=0, verbose_name='接收用户')
        message = models.CharField(max_length=500, verbose_name='消息内容')
        has_read = models.BooleanField(default=False, verbose_name='是否已读')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '用户消息'
            verbose_name_plural = verbose_name
    
    
    class UserCourse(models.Model):
        user = models.ForeignKey(UserProfile, verbose_name='用户')
        course = models.ForeignKey(Course, verbose_name='课程')
        add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    
        class Meta:
            verbose_name = '用户课程'
            verbose_name_plural = verbose_name
    
    
    ###同步###
    
  • 相关阅读:
    SAP Spartacus 自定义Popover指令,如何实现弹出对话框自动关闭功能
    SAP Spartacus B2B 页面信息提示图标的弹出窗口显示实现逻辑
    一个好用的 SAP UI5 本地打包(build)工具,自动生成Component-preload.js
    什么是 SAP UI5 的 Component-preload.js, 什么是Minification和Ugification
    云小课 | 一个三分钟快速定制OCR应用的神器,要不?
    JavaScript实现:如何写出漂亮的条件表达式
    想做测试工程师,这7件事你必须先知道
    比物理线程都好用的C++20的协程,你会用吗?
    解读 SSDB、LevelDB 和 RocksDB 到 GaussDB(for Redis) 的迁移
    数据中心太耗电,送你一个节能神器
  • 原文地址:https://www.cnblogs.com/sellsa/p/8434884.html
Copyright © 2011-2022 走看看