1. 程式人生 > >慕學線上網0.3_四個model

慕學線上網0.3_四個model

1、四個model完整程式碼:

# users/models.py

from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser


class UserProfile(AbstractUser):

    gender_choices = (
        ('male', '男'),
        ('female', '女')
    )

    nick_name = models.CharField('暱稱', max_length=50, default='')
    birthday = models.DateField('生日', null=True, blank=True)
    gender = models.CharField('性別', max_length=10, choices=gender_choices, default='female')
    adress = 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):
    send_choices = (
        ('register', '註冊'),
        ('forget', '找回密碼')
    )

    code = models.CharField('驗證碼', max_length=20)
    email = models.EmailField('郵箱', max_length=50)
    send_type = models.CharField(choices=send_choices, max_length=10)
    send_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = '郵箱驗證碼'
        verbose_name_plural = verbose_name


class Banner(models.Model):
    title = models.CharField('標題', max_length=100)
    image = models.ImageField('輪播圖', upload_to='banner/%Y%m', max_length=100)
    url = models.URLField('訪問地址', max_length=200)
    index = models.IntegerField('順序', default=100)
    add_time = models.DateTimeField('新增時間', default=datetime.now)

    class Meta:
        verbose_name = '輪播圖'
        verbose_name_plural = verbose_name
# course\models.py

from django.db import models
from datetime import datetime
# Create your models here.


class Course(models.Model):
    name = models.CharField(max_length=50, verbose_name="課程名")
    desc = models.CharField(max_length=300, verbose_name="課程描述")
    detail = models.TextField(verbose_name="課程詳情")  # 暫時為TextField
    degree = models.CharField(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="course/%Y/%m", verbose_name="封面圖", max_length=100)
    click_num = 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 Lesson(models.Model):
    course = models.ForeignKey(Course, verbose_name="課程", on_delete=models.CASCADE)  # 刪除關聯資料,與之關聯也刪除
    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 '《{0}》課程的章節 >> {1}'.format(self.course, self.name)


class Video(models.Model):
    lesson = models.ForeignKey(Lesson, verbose_name="章節", on_delete=models.CASCADE)
    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


class CourseResource(models.Model):
    course = models.ForeignKey(Course, verbose_name="課程", on_delete=models.CASCADE)
    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
# organization\models.py
from datetime import datetime

from django.db import models


class CityDict(models.Model):
    name = models.CharField('城市', max_length=20)
    desc = models.CharField('描述', max_length=200)
    add_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = '城市'
        verbose_name_plural = verbose_name


class CourseOrg(models.Model):
    name = models.CharField('機構名稱', max_length=50)
    desc = models.TextField('機構描述')
    click_nums = models.IntegerField('點選數', default=0)
    fav_nums = models.IntegerField('收藏數', default=0)
    image = models.ImageField('封面圖', upload_to='org/%Y%m', max_length=100)
    address = models.CharField('機構地址', max_length=150)
    city = models.ForeignKey(CityDict, verbose_name='所在城市', on_delete=models.CASCADE)
    add_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = '課程機構'
        verbose_name_plural = verbose_name


class Teacher(models.Model):
    org = models.ForeignKey(CourseOrg, verbose_name='所屬機構', on_delete=models.CASCADE)
    name = models.CharField('教師名', max_length=50)
    work_years = models.IntegerField('工作年限', default=0)
    work_company = models.CharField('就職公司', max_length=50)
    work_position = models.CharField('公司職位', max_length=50)
    points = models.CharField('教學特點', max_length=50)
    click_nums = models.IntegerField('點選數', default=0)
    fav_nums = models.IntegerField('收藏數', default=0)
    add_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = '教師'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "[{0}]的教師: {1}".format(self.org, self.name)
# operation\models.py

from datetime import datetime

from django.db import models

from course.models import Course
from users.models import UserProfile


class UserAsk(models.Model):
    name = models.CharField('姓名', max_length=20)
    mobile = models.CharField('手機', max_length=11)
    course_name = models.CharField('課程名', max_length=50)
    add_time = models.DateTimeField('新增時間', default=datetime.now)

    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='使用者', on_delete=models.CASCADE)
    course = models.ForeignKey(Course, verbose_name='課程', on_delete=models.CASCADE)
    comments = models.CharField('評論', max_length=200)
    add_time = models.DateTimeField('新增時間', default=datetime.now)

    class Meta:
        verbose_name = '課程評論'
        verbose_name_plural = verbose_name


class UserFavorite(models.Model):
    FAV_TYPE = (
        (1, '課程'),
        (2, '課程機構'),
        (3, '講師')
    )

    user = models.ForeignKey(UserProfile, verbose_name='使用者', on_delete=models.CASCADE)
    fav_id = models.IntegerField('資料id', default=0)
    fav_type = models.IntegerField(verbose_name='收藏型別', choices=FAV_TYPE, default=1)
    add_time = models.DateTimeField('新增時間', default=datetime.now)

    class Meta:
        verbose_name = '使用者收藏'
        verbose_name_plural = verbose_name


class UserMessage(models.Model):
    user = models.IntegerField('接受使用者', default=0)
    message = models.CharField('訊息內容', max_length=500)
    has_read = models.BooleanField('是否已讀', default=False)
    add_time = models.DateTimeField('新增時間', default=datetime.now)

    class Meta:
        verbose_name = '使用者訊息'
        verbose_name_plural = verbose_name


class UserCourse(models.Model):
    user = models.ForeignKey(UserProfile, verbose_name='使用者', on_delete=models.CASCADE)
    course = models.ForeignKey(Course, verbose_name='課程', on_delete=models.CASCADE)
    add_time = models.DateTimeField('新增時間', default=datetime.now)

    class Meta:
        verbose_name = '使用者課程'
        verbose_name_plural = verbose_name

  2、建立完後記得把App新增到settings的“INSTALLED_APPS”裡面

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
    'course',
    'organization',
    'operation',
]

   3、遷移到資料庫

makemigrations
migrate

   4、將四個App放到一起

  • 新建package:apps
  • 把四個App都拖進去 紅框內的 √ 記得去掉
  • 然後Mark為sourceRoot

  5、然而我們發現,在cmd下執行的話還是會報錯    需要在settings設定:

import os
import sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))

再執行就可以了    未完待續~~~