博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
contentType
阅读量:4614 次
发布时间:2019-06-09

本文共 4738 字,大约阅读时间需要 15 分钟。

 

ContentType组件    解决什么问题:表的id和数据id,来唯一确定一条数据    用:        插入数据:             models.PricePolicy:content_obj = GenericForeignKey('table_id', 'course_id')             views:                    course=models.Course.objects.filter(pk=1).first()                    models.PricePolicy.objects.create(period=9,price=100,content_obj=course)                查询数据:            1 查询所有价格策略,并且显示对应的课程名称:               models.PricePolicy:                        from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation                        content_obj = GenericForeignKey('table_id', 'course_id')               views:                        # ret=models.PricePolicy.objects.all()                        # for price in ret:                        #     print(type(price.content_obj))                        #     print(price.content_obj.title)               2 通过课程id,获取课程信息和价格策略:                modes.Course                    from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation                    policy = GenericRelation('PricePolicy',object_id_field='object_id',content_type_field='content_type')                views:                    # course = models.Course.objects.filter(pk=1).first()                    # print(course.policy.all())                    # for i in course.policy.all():                    #     print(i.price)

 

 

from django.db import models# Create your models here.from django.contrib.contenttypes.models import ContentType# 第二种方式from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelationclass Course(models.Model):    title=models.CharField(max_length=32)    # 三 不会在数据库中生成字段,只用于数据库操作    policy = GenericRelation('PricePolicy',object_id_field='object_id',content_type_field='content_type')class DegreeCourse(models.Model):    title=models.CharField(max_length=32)    # 三 不会在数据库中生成字段,只用于数据库操作    policy = GenericRelation('PricePolicy', object_id_field='object_id', content_type_field='content_type')class PricePolicy(models.Model):    period=models.IntegerField()    price=models.PositiveIntegerField()    content_type=models.ForeignKey(to=ContentType,null=True)  #表名    object_id=models.PositiveIntegerField(null=True) #课程id    # 第二种方式    # 引入一个字段,不会在数据库中创建,只用来做数据库操作    content_obj = GenericForeignKey('content_type', 'object_id')
models.py
from django.shortcuts import render,HttpResponse# Create your views here.from app01 import modelsdef addPrice(request):    '''    为专题课,添加三个价格策略    查询所有价格策略,并且显示对应的课程名称    通过课程id,获取课程信息和价格策略    '''    # 为专题课django课程,添加三个价格策略    # 1 普通方式    # course=models.Course.objects.filter(pk=1).first()    # content=models.ContentType.objects.filter(model='course').first()    # models.PricePolicy.objects.create(period=1,price=0,object_id=course.pk,content_type_id=content.pk)    # models.PricePolicy.objects.create(period=3,price=49,object_id=course.pk,content_type_id=content.pk)    # models.PricePolicy.objects.create(period=6,price=69,object_id=course.pk,content_type_id=content.pk)    # 2 GenericForeignKey    # course=models.Course.objects.filter(pk=1).first()    # models.PricePolicy.objects.create(period=9,price=100,content_obj=course)    # 为Python全站开发学位课,加一个价格策略    # degree = models.DegreeCourse.objects.filter(pk=1).first()    # models.PricePolicy.objects.create(period=2,price=50,content_obj=degree)    # 二 查询所有价格策略,并且显示对应的课程名称    # ret=models.PricePolicy.objects.all()    # for price in ret:    #     print(type(price.content_obj))    #     print(price.content_obj.title)    #三 通过课程id,获取课程信息和价格策略    # course = models.Course.objects.filter(pk=1).first()    # print(course.policy.all())    # for i in course.policy.all():    #     print(i.price)    # 查专题课程 python全站开发    degree = models.DegreeCourse.objects.filter(pk=1).first()    print(degree.policy.all())    for i in degree.policy.all():        print(i.price)    return HttpResponse('ok')
views.py
"""day06 URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:    https://docs.djangoproject.com/en/1.11/topics/http/urls/Examples:Function views    1. Add an import:  from my_app import views    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')Class-based views    1. Add an import:  from other_app.views import Home    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')Including another URLconf    1. Import the include() function: from django.conf.urls import url, include    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))"""from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^test/', views.addPrice),]
urls.py

 

转载于:https://www.cnblogs.com/xujinjin18/p/9846809.html

你可能感兴趣的文章
冲刺周期一--站立会议04
查看>>
支持IE6以上阴影效果纯CSS
查看>>
优化算法与特征缩放
查看>>
NOIP模板复习(4)区间操作之莫队算法,树状数组,线段树
查看>>
深入理解PHP中的引用和赋值
查看>>
58同城2018提前批前端笔试题总结
查看>>
compilation与编译
查看>>
useradd mfs -s /sbin/nologin -M
查看>>
vue+element-ui实现表格checkbox单选
查看>>
box-shadow
查看>>
select * 和select 1 以及 select count(*) 和select count(1)的区别
查看>>
进度条04
查看>>
Silverlight RadGridView的HeaderCellStyle样式
查看>>
IE兼容CSS3圆角border-radius的方法
查看>>
Elsevier期刊投稿状态
查看>>
Heartbeat+LVS构建高可用负载均衡集群
查看>>
多表查询
查看>>
springMVC_数据的处理过程
查看>>
ORM + Mysql配置
查看>>
18 python 初学(time、random 模块)
查看>>