Django组件content-type使用方法详解

yipeiwu_com5年前Python基础

前言

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

django中的一个组件content-type可以帮助我们解决这样的一个问题

在这里我先设计了3张表 学位表 普通课程 和价格策略表 大致的设计如下

在上图中我们可以看到价格策略表和其他的两个表进行了关联,可以根据表明

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')

进行插入数据的类视图

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('...')

为其添加路由

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)

输入以下的地址进行测试

http://127.0.0.1:8000/test

数据库中的结果如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python的面向对象编程方式学习笔记

类与实例 类与实例相互关联着:类是对象的定义,而实例是“真正的实物”,它存放了类中所定义的对象的具体信息。 下面的示例展示了如何创建一个类: class MyNewObjectTyp...

python使用Turtle库绘制动态钟表

python使用Turtle库绘制动态钟表

Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档。同时借助Google和百度。 本文介绍的turtle库对应的官方文档地址 绘制动态钟...

python的类变量和成员变量用法实例教程

本文实例形式讲解了python的类变量和成员变量用法,对于Python程序设计有一定的参考价值。分享给大家供大家参考。具体如下: 先看看下面这段代码: class TestClass...

python基于socket进行端口转发实现后门隐藏的示例

思想: 用户正常浏览器访问请求通过8080端口,请求若为http请求,则正常转发到80端口保证网站正常运行。否则转发到8888端口执行系统命令。 8888端口监听代码: #!/usr...

10 分钟快速入门 Python3的教程

Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计。 它是如今最常用的编程语言之一。它的语法简洁且优美,几乎就是可执行的伪代码。 欢迎大家斧正。英...