其他分享
首页 > 其他分享> > d5

d5

作者:互联网

s7day132

内容回顾:

1.rest组件

2.跨域

今日内容:

1.content-type

​ Django内置的一个组件,帮助开发者做连表操作. [混搭]

​ 帮助生成了所有的表

# 帮助你快速实现content_type操作
content_object = GenericForeignKey('content_type', 'object_id')

​ 反向查找的时候方便

# 仅用于反向查找
price_police_list = GenericRelation('PricePolicy')
# PricePolicy.objects.create(price='9.9', period='30', content_object=obj)
# 和上面的步骤一样, 自动拿到两个id 赋值给两个

views

from django.shortcuts import render, HttpResponse
from app01 import models
# Create your views here.

def test(request):
    """
    # # 1. 为学位课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=15.9, period=60, content_object=obj2)

    obj3 = models.DegreeCourse.objects.filter(title='python全栈').first()
    models.PricePolicy.objects.create(price=22.9, period=90, content_object=obj3)

    # # 1. 为普通课python全栈添加一个价格策略 : 一个月9.9
    obj1 = models.Course.objects.filter(title='restframework').first()
    models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)

    obj2 = models.Course.objects.filter(title='restframework').first()
    models.PricePolicy.objects.create(price=15.9, period=60, content_object=obj2)

    obj3 = models.Course.objects.filter(title='restframework').first()
    models.PricePolicy.objects.create(price=22.9, period=90, content_object=obj3)
    """

    # 3. 根据课程ID获取课程   # 获取该课程的所有价格策略 content_type 封装了方法
    course = models.Course.objects.filter(id=1).first()
    price_policys = course.price_police_list.all()
    print(price_policys)
    # <QuerySet [<PricePolicy: PricePolicy object>, <PricePolicy: PricePolicy object>, <PricePolicy: PricePolicy object>]>
    return HttpResponse('ok')

1568276786745

models

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
# Create your models here.
class Course(models.Model):
    """
    普通课程
    """
    title = models.CharField(max_length=32)

    # 仅用于反向查找
    price_police_list = GenericRelation('PricePolicy')

class DegreeCourse(models.Model):
    """
    学位课程
    """
    title = models.CharField(max_length=32)
    price_police_list = GenericRelation('PricePolicy')

class PricePolicy(models.Model):
    """
    价格策略
    """
    price = models.IntegerField()
    period = models.IntegerField()
    # table_name = models.CharField(verbose_name="关联的表名称")
    # object_id = models.CharField(verbose_name="关联的表中的数据行的ID")

    # content_type 生成的表名称
    content_type = models.ForeignKey(ContentType, verbose_name="关联的表名称")  #7. course  8 . DegreeCourse
    object_id = models.IntegerField(verbose_name="关联的表中的数据行的ID")

    # 帮助你快速实现content_type操作
    content_object = GenericForeignKey('content_type', 'object_id')

# 1. 为学位课python全栈添加一个价格策略 : 一个月9.9
# 1
""" 
obj = DegreeCourse.objects.filter(title='python全栈').first()
# obj.id
cobj = ContentType.objects.filter(models='degreecourse').first()
# cobj.id
PricePolicy.objects.create(price='9.9', period='30', content_type=cobj, object_id=obj.id)
"""
# 2  引入content_object = GenericForeignKey('content_type', 'object_id')
# obj = DegreeCourse.objects.filter(title='python全栈').first()
# PricePolicy.objects.create(price='9.9', period='30', content_object=obj)
# 和上面的步骤一样, 自动拿到两个id 赋值给两个

2.路飞学城(在线教育平台)

公司规模:

部门:

开发:

测试: 1
运维: 1
产品经理: 1
UI:1
运营:1
销售:2
全职导师:2

开发的周期:

第一期: 6个月, 基本的功能,能用(前两个月能用了,但是出现bug)

第二期: 完善功能

第三期:

开发:

1.需求分析

2.原型+UI

3.数据库设计

4.功能开发

1568279154940

标签:models,price,object,content,objects,PricePolicy,d5
来源: https://www.cnblogs.com/Doner/p/11514272.html