浅谈Python之Django(二)
作者:z-victor
django设计
user-用户管理
curser-课程管理
organization-机构和教师管理
数据表生成与修改各APP models设计
users models 设计
修改users目录下的models
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models from django.contrib.auth.models import AbstractUser # Create your models here. class UserProfile(AbstractUser): # verbose_name:备注 # blank:是否可以为空 # nick_name = models.CharField(max_length=50,verbose_name=u"昵称",default="") birthday = models.DateField(verbose_name=u"生日",blank=True) gender = models.CharField(choices=(('male',u'男'),('female',u'女')),default="female",max_length=6) 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) class Meta: verbose_name = u"用户信息" verbose_name_plural = verbose_name def __unicode__(self): return self.username class EmailVerifyRecord(models.Model): code = models.CharField(max_length=20,verbose_name=u"验证码") email = models.EmailField(max_length=50,verbose_name=u"邮箱") send_type = models.CharField(max_length=10,choices=(("register",u"注册"),("forget",u"找回密码")),) send_time = models.DateTimeField(default=datetime.now(),verbose_name=u"添加时间") class Meta: verbose_name = u"邮箱验证码" verbose_name_plural = verbose_name class Banner(models.Model): title = models.CharField(max_length=100,verbose_name=u"标题") image = models.ImageField(max_length=100,upload_to="banner/%Y/%m",verbose_name=u"轮播图") url = models.URLField(max_length=100,verbose_name=u"访问地址") index = models.IntegerField(verbose_name=u"顺序",default=100) add_time = models.DateTimeField(default=datetime.now(),verbose_name=u"添加时间") class Meta: verbose_name = u"轮播图" verbose_name_plural = verbose_name
修改配置文件setting.py中的INSTALLED_APPS,将我们的app users加入,添加AUTH_USER_MODEL = “users.UserProfile”
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', ] AUTH_USER_MODEL = "users.UserProfile"
运行python manage.py runserver 0.0.0.0:9000
解决问题:
pip3 install Pillow(安装Pillow包)
python manage.py makemigrations users
python manage.py migrate users
解决users.0001_initial,将配置文件中的INSTALLED_APPS中的django.contrib.admin注释掉
course models 设计
修改course 目录下的models
# _*_ encoding: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(max_length=2,choices=(('cj','初级'),('zj','中级'),('gj','高级'))) 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(max_length=100,verbose_name="封面图片",upload_to="courses/%Y/%m") 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"课程",on_delete=models.CASCADE) 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"章节",on_delete=models.CASCADE) 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"课程",on_delete=models.CASCADE) name = models.CharField(max_length=100, verbose_name=u"名称") download = models.FileField(upload_to="course/recourse/%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
修改配置文件setting.py中的INSTALLED_APPS,将我们的app course加入
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 'course', ]
运行python manage.py runserver 0.0.0.0:9000
原因
在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: init() missing 1 required positional argument: ‘on_delete'
涉及到外键都需要改成
course = models.ForeignKey(Course,verbose_name=u"课程",on_delete=models.CASCADE)
organization models 设计
修改organization目录下的models
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models # Create your models here. class CityDict(models.Model): name = models.CharField(max_length=20, verbose_name=u"城市名称") desc = models.CharField(max_length=200,verbose_name=u"城市描述") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"城市" verbose_name_plural = verbose_name class CourseOrg(models.Model): name = models.CharField(max_length=50,verbose_name=u"机构名称") desc = models.TextField(verbose_name=u"机构描述") click_nums = models.IntegerField(default=0,verbose_name=u"点击数") fav_nums = models.IntegerField(default=0,verbose_name=u"收藏数") image = models.ImageField(upload_to="org/%Y/%m",verbose_name=u"封面图",max_length=100) address = models.CharField(max_length=150,verbose_name=u"机构地址") city = models.ForeignKey(CityDict,verbose_name=u"所在城市",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"课程机构" verbose_name_plural = verbose_name class Teacher(models.Model): org = models.ForeignKey(CourseOrg,verbose_name=u"所属机构",on_delete=models.CASCADE) name = models.CharField(max_length=50, verbose_name=u"教师名称") work_years = models.IntegerField(default=0,verbose_name=u"工作年限") work_company = models.CharField(max_length=50,verbose_name=u"就职公司") work_position = models.CharField(max_length=50,verbose_name=u"公司职位") points = models.CharField(max_length=50,verbose_name=u"教学特点") click_nums = models.IntegerField(default=0, verbose_name=u"点击数") fav_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
修改配置文件setting.py中的INSTALLED_APPS,将我们的app organization加入
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 'course', 'organization', ]
operation models 设计
# _*_ encoding:utf-8 _*_from __future__ import unicode_literalsfrom datetime import datetimefrom django.db import modelsfrom users.models import UserProfilefrom course.models import Course# Create your models here.class UserAsk(models.Model): name = models.CharField(max_length=20,verbose_name=u"姓名") mobile = models.CharField(max_length=11,verbose_name=u"手机") course_name = models.CharField(max_length=50,verbose_name=u"课程名") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间") class Meta: verbose_name = u"用户咨询" verbose_name_plural = verbose_nameclass CourseComments(models.Model): # 课程评论 user = models.ForeignKey(UserProfile,verbose_name=u"用户",on_delete=models.CASCADE) course = models.ForeignKey(Course,verbose_name=u"课程",on_delete=models.CASCADE) comments = models.CharField(max_length=200,verbose_name=u"评论") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"评论时间") class Meta: verbose_name = u"课程评论" verbose_name_plural = verbose_nameclass UserFavorite(models.Model): user = models.ForeignKey(UserProfile,verbose_name=u"用户",on_delete=models.CASCADE) fav_id = models.IntegerField(default=0,verbose_name=u"数据id") fav_type = models.IntegerField(choices=((1,'课程'),(2,'课程机构'),(3,'讲师')),default=1, verbose_name=u"收藏类型") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"收藏时间") class Meta: verbose_name = u"用户收藏" verbose_name_plural = verbose_nameclass UserMessage(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"接收用户",on_delete=models.CASCADE) message = models.CharField(max_length=500,verbose_name=u"消息内容") has_read = models.BooleanField(default=False,verbose_name=u"是否已读") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间") class Meta: verbose_name = u"用户信息" verbose_name_plural = verbose_nameclass UserCourse(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用户",on_delete=models.CASCADE) course = models.ForeignKey(Course, verbose_name=u"课程",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"用户课程" verbose_name_plural = verbose_name
修改配置文件setting.py中的INSTALLED_APPS,将我们的app operation加入
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models from users.models import UserProfile from course.models import Course # Create your models here. class UserAsk(models.Model): name = models.CharField(max_length=20,verbose_name=u"姓名") mobile = models.CharField(max_length=11,verbose_name=u"手机") course_name = models.CharField(max_length=50,verbose_name=u"课程名") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间") class Meta: verbose_name = u"用户咨询" verbose_name_plural = verbose_name class CourseComments(models.Model): # 课程评论 user = models.ForeignKey(UserProfile,verbose_name=u"用户",on_delete=models.CASCADE) course = models.ForeignKey(Course,verbose_name=u"课程",on_delete=models.CASCADE) comments = models.CharField(max_length=200,verbose_name=u"评论") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"评论时间") class Meta: verbose_name = u"课程评论" verbose_name_plural = verbose_name class UserFavorite(models.Model): user = models.ForeignKey(UserProfile,verbose_name=u"用户",on_delete=models.CASCADE) fav_id = models.IntegerField(default=0,verbose_name=u"数据id") fav_type = models.IntegerField(choices=((1,'课程'),(2,'课程机构'),(3,'讲师')),default=1, verbose_name=u"收藏类型") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"收藏时间") class Meta: verbose_name = u"用户收藏" verbose_name_plural = verbose_name class UserMessage(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"接收用户",on_delete=models.CASCADE) message = models.CharField(max_length=500,verbose_name=u"消息内容") has_read = models.BooleanField(default=False,verbose_name=u"是否已读") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间") class Meta: verbose_name = u"用户信息" verbose_name_plural = verbose_name class UserCourse(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用户",on_delete=models.CASCADE) course = models.ForeignKey(Course, verbose_name=u"课程",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间") class Meta: verbose_name = u"用户课程" verbose_name_plural = verbose_name
将4个app统一放到apps包下
因为app太多,不好管理,所以统一放到apps目录下进行管理
将app路径写入到配置文件setting.py中,防止包导入失败
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'))
python manage.py makemigrations
python manage.py migrate
生成数据表
> 运行python manage.py runserver 0.0.0.0:9000
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!