10.operation的models

startapp operation 后

from datetime import datetime

from django.db import models

from users.models import UserProfile
from courses.models import Course

# Create your models here.

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

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="接收用户") # 代表用户id
    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

然后在installed_apps里添加
'users',
'courses',
'organization',
'operation',
然后再makemigrations和migrate,这样数据表就生成了。但是几个app的目录都直接放在根目录下显得比较乱,可以新建一个apps的文件夹放这几个。在根目录下新建完之后拖进去。这样路径变了,需要在settings.py里设置。

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

的意思是项目的根目录。
我们import sys再写:

sys.path.insert(0,os.path.join(BASE_DIR,"apps"))

这样就把apps的目录和BASE_DIR连接起来了,这样在根目录搜索app时候也可搜到apps路径了。其中0是位置参数,基地一个位置。然后老师还做了个事,把每个app里的migrations里的0001_initial.py里涉及到apps.的路径全删了,否则会报错,因为刚才在settings改路径这里再写就重复了。但是我的这里没有,可能是因为用的python3.6的原因。