Django REST framework 项目笔记
1.模拟学城项目数据库表设计之content-type印子


models.py表你可能想这么建
from django.db import models
class Course(models.Model):
title=models.CharField(max_length=32)
class DegreeCourse(models.Model):
title=models.CharField(max_length=32)
class PricePolicy(models.Model):
price=models.IntegerField()
period=models.IntegerField() # 周期
rable_name=models.CharField(verbose_name='关联的表名称')
object_id=models.CharField(verbose_name='关联的表中的数据行的ID')
其实不用这样建,我们用contenttypes,我可以在配置文件中找到
INSTALLED_APPS = [
'django.contrib.contenttypes',
]
views.py中引入并应用
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
class Course(models.Model):
"""
普通课
"""
title=models.CharField(max_length=32)
class DegreeCourse(models.Model):
"""
学位课
"""
title=models.CharField(max_length=32)
class PricePolicy(models.Model):
"""
价格策略
"""
price=models.IntegerField()
period=models.IntegerField() # 周期
content_type = models.ForeignKey(ContentType, verbose_name='关联的表名称')
object_id = models.IntegerField(verbose_name='关联的表中的数据行的ID')
content_object=GenericForeignKey('content_type','object_id')
插入两天课程数据


为学位Python添加一个价格策略 一个月9.9
from django.shortcuts import render, HttpResponse
from app01 import models
def test(request):
# 为学位Python添加一个价格策略 一个月9.9
obj1 = models.DegreeCourse.objects.filter(title="Python全栈").first()
models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
obj2 = models.DegreeCourse.objects.filter(title="Python全栈").first()
models.PricePolicy.objects.create(price=19.9, period=40, content_object=obj2)
obj3 = models.DegreeCourse.objects.filter(title="Python全栈").first()
models.PricePolicy.objects.create(price=29.9, period=50, content_object=obj3)
return HttpResponse('ok')

为学位课“linux”添加一个价格策略:一个月 9.9
from django.shortcuts import render, HttpResponse
from app01 import models
def test(request):
# 2. 为学位课“linux”添加一个价格策略:一个月 9.9
obj1 = models.Course.objects.filter(title='linux').first()
models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
obj2 = models.Course.objects.filter(title='linux').first()
models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
obj3 = models.Course.objects.filter(title='linux').first()
models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)
return HttpResponse('ok')

根据课程ID获取课程, 并获取该课程的所有价格策略
改写models.py
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
class Course(models.Model):
"""
普通课
"""
title = models.CharField(max_length=32)
# 仅用于反向查找
price_policy_list = GenericRelation("PricePolicy")
class DegreeCourse(models.Model):
"""
学位课
"""
title = models.CharField(max_length=32)
# 仅用于反向查找
price_policy_list = GenericRelation("PricePolicy")
class PricePolicy(models.Model):
"""
价格策略
"""
price = models.IntegerField()
period = models.IntegerField() # 周期
content_type = models.ForeignKey(ContentType, verbose_name='关联的表名称')
object_id = models.IntegerField(verbose_name='关联的表中的数据行的ID')
# 帮助你快速实现content_type操作
content_object = GenericForeignKey('content_type', 'object_id')
views.py
from django.shortcuts import render, HttpResponse
from app01 import models
def test(request):
# 3. 根据课程ID获取课程, 并获取该课程的所有价格策略
course = models.Course.objects.filter(id=1).first()
price_policys = course.price_policy_list.all()
print(price_policys)
return HttpResponse(price_policys)
打印结果策略:<QuerySet [<PricePolicy: PricePolicy object>, <PricePolicy: PricePolicy object>, <PricePolicy: PricePolicy object>]>
2.路飞学城项目(在线教育平台)
小插曲
公司规模:
部门:
开发:
主站: 2 + 1(给用户用)
导师后台:1 (给导师用)
管理后台:1 (给运营用)
测试:1 代码承载的并发量,PV指页面被访问了多少次,UV指用户访问了多少次
运维:1
产品经理:1
UI:1
运营:1 (刚开始还写微信公众号文章,加密,上传视频等等)
销售:2
全职导师:2
开发周期:
三期: 一期基本功能 ,占领市场 (5个月) 二期 ...,三期...
开发阶段
1.需求分析
2.原型+UI
3.数据库设计
4.功能开发(红色是需要重点掌握的)
- 主站业务(主要负责哪一类的)
- 课程系列(课程列表,课程详细,价格策略,章节,观看视频,用户评价,常见问题)
-深科技 (文章列表,文章详细,点赞,收藏,评论)
- 用户的登录注册,个人中心
-购物
- 加入购物车
- 结算
-立即支付(基于支付宝)
-消息推送(基于微信)
- 前端VUE
-前端所有

浙公网安备 33010602011771号