django之contenttype组件

django组件contenttype使用条件:

  一张表要和n张表创建 ForeignKey 关系时;作用:可以通过两个字段(表名称和行ID)让表和N张表创建FK关系

示例:

models.py:(表结构)

复制代码
from django.db import models
from django.contrib.contenttypes.models import ContentType

from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation


class DegreeCourse(models.Model):
    """学位课程"""
    name = models.CharField(max_length=128, unique=True)
    course_img = models.CharField(max_length=255, verbose_name="缩略图")
    brief = models.TextField(verbose_name="学位课程简介", )


class Course(models.Model):
    """专题课程"""
    name = models.CharField(max_length=128, unique=True)
    course_img = models.CharField(max_length=255)

    # 不会在数据库生成列,只用于帮助你进行查询
    policy_list = GenericRelation("PricePolicy")


class PricePolicy(models.Model):
    """价格与有课程效期表"""
    content_type = models.ForeignKey(ContentType)  # 关联course or degree_course
    object_id = models.PositiveIntegerField()

    # content_object这个字段只用于帮助你进行添加和查询,不会在数据库生成列
    content_object = GenericForeignKey('content_type', 'object_id')   # 相当于把 "content_type"和"object_id" 这两个字段放到了 GenericForeignKey 的对象中了


    valid_period_choices = (
        (1, '1天'),
        (3, '3天'),
        (7, '1周'), (14, '2周'),
        (30, '1个月'),
        (60, '2个月'),
        (90, '3个月'),
        (180, '6个月'), (210, '12个月'),
        (540, '18个月'), (720, '24个月'),
    )
    valid_period = models.SmallIntegerField(choices=valid_period_choices)
    price = models.FloatField()
复制代码

views.py:(使用)

复制代码
from django.shortcuts import render,HttpResponse
from app01 import models
from django.contrib.contenttypes.models import ContentType

def test(request):
    # 1.在价格策略表中添加一条数据
    # models.PricePolicy.objects.create(
    #     valid_period=7,
    #     price=6.6,
    #     content_type=ContentType.objects.get(model='course'),
    #     object_id=1
    # )

    # models.PricePolicy.objects.create(
    #     valid_period=14,
    #     price=9.9,
    #     content_object=models.Course.objects.get(id=1)  # GenericForeignKey 添加操作; 找到 Course表 的表名和id 赋值给content_type,取到记录行的id 赋值给 object_id,然后自动增加
    # )

    # 2. 根据某个价格策略对象,找到他对应的表和数据,如:管理课程名称
    # price = models.PricePolicy.objects.get(id=2)  # GenericForeignKey 查询操作
    # print(price.content_object.name) # 自动帮你找到

    # 3.找到某个课程关联的所有价格策略
    # obj = models.Course.objects.get(id=1)
    # for item in obj.policy_list.all():      # GenericRelation 查询操作
    #     print(item.id,item.valid_period,item.price)
    #
    return HttpResponse('...')
复制代码
原文地址:https://www.cnblogs.com/longfeiwang/p/11085435.html