Django入门4 数据库设计
创建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
> makemigrations users > migrate users
新建课程app course app
> 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
> startapp organization
新建操作app
> startapp operation
写好各个models后,进行settings.py配置
生成表
> makemigrations
> 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‘))
这样在命令行下就可以正常运行了
相关推荐
gaozhennan 2020-08-16
chenjiazhu 2020-07-08
xiaoxiangyu 2020-06-28
helencoder 2020-06-26
踩风火轮的乌龟 2020-06-02
nan00zzu 2020-05-17
xwb 2020-05-16
dongxurr 2020-04-21
happinessaflower 2020-04-18
zhengsj 2020-04-15
neverstopforcode 2020-04-14
喝绿茶的猫 2020-03-28
踩风火轮的乌龟 2020-03-23
nan00zzu 2020-03-08
whyname 2020-03-07
CharlesYooSky 2020-01-07