01_数据库表设计

Posted zhaye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01_数据库表设计相关的知识,希望对你有一定的参考价值。

一、使用Pycharm创建项目

技术图片

技术图片

二、在 models.py 中创建所有表

技术图片
from django.contrib.auth.models import User
from django.db import models


# Create your models here.
class UserProfile(models.Model):
    """用户信息表"""
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=64, verbose_name="姓名")
    role = models.ManyToManyField("Role", blank=True, null=True)

    def __str__(self):
        return self.name


class Role(models.Model):
    """角色表"""
    name = models.CharField(max_length=64, unique=True)

    def __str__(self):
        return self.name


class CustomerInfo(models.Model):
    """客户信息表"""
    name = models.CharField(max_length=64, default=None)
    contact_type_choices = ((0, qq), (1, 微信), (2, 手机))
    contact_type = models.SmallIntegerField(choices=contact_type_choices, default=0)
    contact = models.CharField(max_length=64, unique=True)
    source_choices = ((0, QQ群), (1, 51CTO), (2, 百度推广), (3, 知乎), (4, 转介绍), (5, 其他))
    source = models.SmallIntegerField(choices=source_choices)
    referral_from = models.ForeignKey("self", blank=True, null=True, verbose_name="转介绍", on_delete=models.SET_NULL)
    consult_courses = models.ManyToManyField("Course", verbose_name="咨询课程")
    consult_content = models.TextField(verbose_name="咨询内容")
    status_choices = ((0, 未报名), (1, 已报名), (2, 已退学))
    status = models.SmallIntegerField(choices=status_choices)
    consultant = models.ForeignKey("UserProfile", verbose_name="课程顾问", on_delete=models.CASCADE)
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.name


class Student(models.Model):
    """学员表"""
    customer = models.ForeignKey("CustomerInfo", on_delete=models.CASCADE)
    class_grades = models.ManyToManyField("ClassList")

    def __str__(self):
        return self.customer


class CustomerFollowUp(models.Model):
    """客户跟踪记录表"""
    customer = models.ForeignKey("CustomerInfo", on_delete=models.CASCADE)
    content = models.TextField(verbose_name="跟踪内容")
    user = models.ForeignKey("UserProfile",on_delete=models.CASCADE)
    status_choices = ((0, 近期无报名计划),
                      (1, 一个月内报名),
                      (2, 2周内报名),
                      (3, 已报名))
    status = models.SmallIntegerField(choices=status_choices)

    def __str__(self):
        return self.content


class Course(models.Model):
    """课程表"""
    name = models.CharField(max_length=64, verbose_name="课程名称", unique=True)
    price = models.PositiveSmallIntegerField()
    period = models.PositiveSmallIntegerField(verbose_name="课程周期(月)", default=5)
    outline = models.TextField(verbose_name="大纲")


class ClassList(models.Model):
    """班级列表"""
    branch = models.ForeignKey("Branch", on_delete=models.CASCADE)
    course = models.ForeignKey("Course", on_delete=models.CASCADE)
    class_type_choices = ((0, 脱产), (1, 周末), (2, 网络班))
    class_type = models.SmallIntegerField(choices=class_type_choices, default=0)
    semester = models.PositiveSmallIntegerField(verbose_name="学期")
    teachers = models.ManyToManyField("UserProfile", verbose_name="讲师")
    start_date = models.DateField("开班日期")
    graduate_date = models.DateField("毕业日期", blank=True, null=True)

    def __str__(self):
        return "%s(%s)期" % (self.course.name, self.semester)

    class Meta:
        unique_together = (branch, class_type, course, semester)


class CourseRecord(models.Model):
    """上课记录"""
    class_grade = models.ForeignKey("ClassList", verbose_name="上课班级", on_delete=models.CASCADE)
    day_num = models.PositiveSmallIntegerField(verbose_name="课程节次")
    teacher = models.ForeignKey("UserProfile", verbose_name="本节讲师", on_delete=models.CASCADE)
    title = models.CharField("本节主题", max_length=64)
    content = models.TextField("内容")
    has_homework = models.BooleanField("本节有作业", default=True)
    homework = models.TextField("作业需求", blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s第(%s)节" % (self.class_grade, self.day_num)

    class Meta:
        unique_together = (class_grade, day_num)


class StudyRecord(models.Model):
    """学习记录表"""
    course_record = models.ForeignKey("CourseRecord", on_delete=models.CASCADE)
    student = models.ForeignKey("Student", on_delete=models.CASCADE)

    score_choices = (
        (100, "A+"), (90, "A"), (85, "B+"), (80, "B"), (75, "B-"), (70, "C+"), (60, "C"), (40, "C-"), (-50, "D"),
        (0, "N/A"), (-100, "COPY"))
    score = models.SmallIntegerField("成绩等级", choices=score_choices, default=0)
    show_choices = ((0, 缺勤), (1, 已签到), (2, 迟到), (3, 早退))
    show = models.SmallIntegerField("考勤", choices=show_choices, default=1)
    note = models.TextField("成绩备注", blank=True, null=True)

    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "%s %s %s" % (self.course_record, self.student, self.score)


class Branch(models.Model):
    """校区"""
    name = models.CharField(max_length=64, unique=True)
    addr = models.CharField(max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name
models.py

此项目使用的是 系统自带的 sqlite 数据库,所以使用默认配置

三、在终端中执行以下命令创建表

python manage.py makemigrations
python manage.py migrate

四、在 admin.py 中注册

技术图片
from django.contrib import admin
from crm import models

# Register your models here.
admin.site.register(models.CustomerInfo)
admin.site.register(models.CustomerFollowUp)
admin.site.register(models.ClassList)
admin.site.register(models.Course)
admin.py

五、创建 admin 后台管理

输入用户名 ren 密码 root

技术图片

启动项目,打开:http://127.0.0.1:8000/admin/ 就可以进管理后台了

以上是关于01_数据库表设计的主要内容,如果未能解决你的问题,请参考以下文章

是否有在单个活动中处理多个片段的 Android 设计模式?

如何更改谷歌地图标记上方的标题和片段设计

HashMap原理:哈希函数的设计

抽屉之Tornado实战--数据库表设计

抽屉之Tornado实战--数据库表设计

将数据从底部工作表对话框片段传递到片段