Django组件content-type使用方法详解
前言
参考博客: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使用方法详解的更多相关文章
- 转载 JS组件Bootstrap Select2使用方法详解
JS组件Bootstrap Select2使用方法详解 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2016-01-26我要评论 这篇文章主要为大家介绍了JS组件Bootstrap Sel ...
- Bootstrap Table使用方法详解
http://www.jb51.net/article/89573.htm bootstrap-table使用总结 bootstrap-table是在bootstrap-table的基础上写出来的,专 ...
- Python的Django框架中forms表单类的使用方法详解
用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...
- vc中调用Com组件的方法详解
vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口: IGetRes--方法Hello(), IGet ...
- asp.net iis URLRewrite 实现方法详解
原文 asp.net iis URLRewrite 实现方法详解 实现非常简单首先你要在你的项目里引用两个dll:actionlessform.dll.urlrewriter.dll,真正实现重写的是 ...
- CURL使用方法详解
php采集神器CURL使用方法详解 作者:佚名 更新时间:2016-10-21 对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...
- PHP cURL应用实现模拟登录与采集使用方法详解
对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...
- $.ajax()方法详解 jquery
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...
- jQuery中 $.ajax()方法详解
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...
- PHP cURL实现模拟登录与采集使用方法详解教程
来源:http://www.zjmainstay.cn/php-curl 本文将通过案例,整合浏览器工具与PHP程序,教你如何让数据 唾手可得 . 对于做过数据采集的人来说,cURL一定不会陌生.虽然 ...
随机推荐
- 当git上只做文件大小写重命名的修改时,如何躲坑
一. 提交时 假设修改ABC.java为Abc.java. 1.1 如果使用git命令进行仅涉及大小写的重命名 1.1.1 设置git库为大小写敏感(不建议) $ git config core.ig ...
- VIM 批量缩进4个空格
vim /etc/vimrc 或 vim ~/.vimrc set smartindent set shiftwidth= 按v选中多行,回车 然后shifit + >
- 【Linux常见命令】cut命令
cut - remove sections from each line of files 参数: -b 可以按字节来查看文件中的内容 -b参数用在中文上,容易出现乱码问题.因为中文字符一个字符占两个 ...
- 数学--数论--HDU1576 A / B(逆元)
问题描述 要求(A / B)%9973,但由于A很大,我们只被告知n(n = A%9973)(我们给定的A必能被B整除,且gcd(B,9973)= 1). 输入项 数据的第一行是一个T,表示有T组数据 ...
- 虚拟机上图片服务器搭建(FastDFS+nginx)
文件服务器 0.提前建好需要的文件夹(/home/fastdfs) /home/fastdfs/tracker /home/fastdfs/storage /home/fastdfs/storage/ ...
- python字符串分段组合(更新)
描述 获得输入的一个字符串s,以字符减号(-)分割s,将其中首尾两段用加号(+)组合后输出. ...
- ssrf爆破mysql
php ssrf 代码<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $_GET['url']); #curl_setopt($ch ...
- 线段树的区间合并 B - LCIS
B - LCIS HDU - 3308 这个是一个很简单很明显的线段树的区间合并,不过区间合并的题目都还是有点难写,建议存个板子. #include <cstdio> #include & ...
- 06_CSS入门和高级技巧(4)
复习 CSS : 负责样式层,层叠式样式表cascading style sheet.CSS2.1,最新版本CSS3. CSS选择器: 选择哪些元素加样式.基本选择3种:标签p.id选择器#id.cl ...
- Docker知识点整理
目录 1. Docker简介 1.1 Docker是什么 1.2 在隔离的容器中运行软件 1.3 分发容器 2. Docker镜像 2.1 Docker镜像简介 2.2 Docker镜像常见操作 2. ...