码迷,mamicode.com
首页 > 数据库 > 详细

Django入门4 数据库设计

时间:2019-12-16 13:40:45      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:mysq   integer   高级   信息   odi   settings   erb   环境   mic   

创建mxonline虚拟环境

C:\Users\ws>mkvirtualenv mxonline

技术图片

(mxonline) D:\python\djangostart>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl

修改settings.py配置

技术图片

创建mxonline数据库

生成django默认表

Makemigrations

migrate

技术图片

新建users app

技术图片

编写users的model

# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals

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

# Create your models here.


class UserProfile(AbstractBaseUser):    # 利用django的默认user表扩展我们的user表
    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=5, choices=(("male",""),("female","")), default="femaile")
    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)

    # 要加如下两行,否则会报错 AttributeError: type object ‘UserProfile‘ has no attribute ‘USERNAME_FIELD‘
    identifier = models.CharField(max_length=40, unique=True)
    USERNAME_FIELD = identifier

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.username
manage.py@mxonline > makemigrations users
manage.py@mxonline > migrate users

技术图片

新建课程app course app

manage.py@mxonline > startapp courses

编写课程models

course/models.py

# _*_ coding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime

from django.db import models

# Create your models here.


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(choices=(("cj","初级"),("zj","中级"),("gj","高级")), max_length=2)
    learn_time = 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


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

新建组织app

manage.py@mxonline > startapp organization

 

新建操作app

manage.py@mxonline >  startapp operation

 

写好各个models后,进行settings.py配置

生成表

manage.py@mxonline > makemigrations

manage.py@mxonline > migrate

技术图片

技术图片

此时会在每个app下面生成0001_inital.py

技术图片

可以看到django生成的表是app+app里面的class全部小写拼接而成

技术图片

我们建的app比较多,为了便于管理,我们在项目目录下新建apps包,把users,course,ogranaziton和operation拖入apps中

把apps加入到编辑器的resource root

技术图片

Settings.py中配置加入到搜索目录
sys.path.insert(0, os.path.join(BASE_DIR, ‘apps‘))

 技术图片

这样在命令行下就可以正常运行了

技术图片

Django入门4 数据库设计

标签:mysq   integer   高级   信息   odi   settings   erb   环境   mic   

原文地址:https://www.cnblogs.com/reblue520/p/12048350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!