前言

  参考博客:https://www.zhangshengrong.com/p/zD1yQJwp1r/

  一个表和多个表进行关联,但具体随着业务的加深,表不断的增加,关联的数量不断的增加,怎么通过一开始通过表的设计后,不在后期在修改表,彻底的解决这个问题呢?

django中的一个组件content-type可以帮助我们解决这样的一个问题。在这里我先设计了3张表:学位表、 普通课程 和价格策略表 ,价格策略表和其他的两个表进行了关联,可以根据表content-type进行关联。

  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='关联的表名称') # 7,8 表名称
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): # 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=19.9, period=60, content_object=obj2)
#
# obj3 = models.DegreeCourse.objects.filter(title='Python全栈').first()
# models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3) # 2. 为学位课“rest”添加一个价格策略:一个月 9.9
# obj1 = models.Course.objects.filter(title='rest framework').first()
# models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
#
# obj2 = models.Course.objects.filter(title='rest framework').first()
# models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
#
# obj3 = models.Course.objects.filter(title='rest framework').first()
# models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3) # 3. 根据课程ID获取课程, 并获取该课程的所有价格策略
# course = models.Course.objects.filter(id=1).first()
#
# price_policys = course.price_policy_list.all()
#
# print(price_policys) return HttpResponse('...')

  urls.py为其添加路由

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.test),
]

  自行插入数据可能会这样写  

# 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9
"""
obj = DegreeCourse.objects.filter(title='Python全栈').first()
# obj.id
cobj = ContentType.objects.filter(model='course').first()
# cobj.id
PricePolicy.objects.create(price='9.9',period='30',content_type_id=cobj.id,object_id=obj.id)
"""
# obj = DegreeCourse.objects.filter(title='Python全栈').first()
# PricePolicy.objects.create(price='9.9',period='30',content_object=obj)

手动插入方式

Django组件content-type使用方法详解的更多相关文章

  1. 转载 JS组件Bootstrap Select2使用方法详解

    JS组件Bootstrap Select2使用方法详解 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2016-01-26我要评论 这篇文章主要为大家介绍了JS组件Bootstrap Sel ...

  2. Bootstrap Table使用方法详解

    http://www.jb51.net/article/89573.htm bootstrap-table使用总结 bootstrap-table是在bootstrap-table的基础上写出来的,专 ...

  3. Python的Django框架中forms表单类的使用方法详解

    用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...

  4. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  5. asp.net iis URLRewrite 实现方法详解

    原文 asp.net iis URLRewrite 实现方法详解 实现非常简单首先你要在你的项目里引用两个dll:actionlessform.dll.urlrewriter.dll,真正实现重写的是 ...

  6. CURL使用方法详解

    php采集神器CURL使用方法详解 作者:佚名  更新时间:2016-10-21   对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...

  7. PHP cURL应用实现模拟登录与采集使用方法详解

    对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...

  8. $.ajax()方法详解 jquery

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  9. jQuery中 $.ajax()方法详解

    $.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...

  10. PHP cURL实现模拟登录与采集使用方法详解教程

    来源:http://www.zjmainstay.cn/php-curl 本文将通过案例,整合浏览器工具与PHP程序,教你如何让数据 唾手可得 . 对于做过数据采集的人来说,cURL一定不会陌生.虽然 ...

随机推荐

  1. SQLI-LABS学习笔记(四)

    第十六关   和之前的关卡一样,修改闭合,无意义的关卡   ")闭合即可   第十七关   这题从源码上看发现     这里进行了两次查询   先查询了用户名是否存在   再查询密码是否匹配 ...

  2. MAC攻击及缺陷

    MAC攻击及缺陷 MAC有好几种实现方式 对MAC的攻击 重放攻击 重放攻击的防护 密钥推测攻击 MAC算法的缺陷 第三方证明 防止否认 前面我们在讲HMAC的时候简单讲过了什么是MAC消息认证码. ...

  3. CodeForces 674C Levels and Regions

    #include<bits/stdc++.h> using namespace std; const int maxn=2e5+5; int N,K,head,tair; int q[ma ...

  4. 【Linux常见命令】echo命令

    echo - display a line of text 打印输出内容的常用命令,可以结合重定向>和追加>>对文件进行覆盖或追加内容. 语法: echo [SHORT-OPTION ...

  5. 小猪的Python学习之旅 —— 16.再尝Python数据分析:采集拉勾网数据分析Android就业行情...

    一句话概括本文: 爬取拉钩Android职位相关数据,利用numpy,pandas和matplotlib对招人公司 情况和招聘要求进行数据分析. 引言: 在写完上一篇<浅尝Python数据分析: ...

  6. Spring LDAP的使用

    LDAP入门http://www.jianshu.com/p/7e4d99f6baaf Spring LDAP,是Spring的一个组件,实现对LDAP的操作. 在编程操作MySQL时,我们除了用JD ...

  7. CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)

    ACM思维题训练集合 The Little Elephant has got a problem - somebody has been touching his sorted by non-decr ...

  8. 数学--数论--HDU 1098 Ignatius's puzzle (费马小定理+打表)

    Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so h ...

  9. P1458 顺序的分数 Ordered Fractions(有技巧的枚举)+C++类封装=精简代码

    题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1,请找出所有满足条件的分数. 这有一个例子,当N=5时,所有解 ...

  10. python基础入门教程(一条龙服务)

    一.语言基础 01-1 计算机系统 解释器下载 变量   小整数池 01-2 垃圾回收机制 02 数据类型 运算符(解压赋值等) 03 流程控制 if while for 04 整形 字符串 列表 0 ...