zoukankan      html  css  js  c++  java
  • contentType

    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,GenericRelation
    
    class 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 models
    
    def 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 Configuration
    
    The `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 url
    from django.contrib import admin
    from app01 import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^test/', views.addPrice),
    ]
    urls.py
  • 相关阅读:
    spring_150807_hibernate_transaction_annotation
    快速排序算法
    组合数递推算法
    HDU 4832 Chess(DP+组合数)
    HDU 2602 Bone Collector (01背包)
    HDU 1597 find the nth digit (二分查找)
    HDU1163 Eddy's digital Roots(九余数定理)
    HDU1031 Design T-Shirt (二级排序)
    HDU1719 Friend (数学推导)
    HDU1720 A+B Coming (16进制加法)
  • 原文地址:https://www.cnblogs.com/xujinjin18/p/9846809.html
Copyright © 2011-2022 走看看