• Django之contenttyes组件


    适用场景

      应用场景:若一张表与多张表都有关联的情况下

    之前表与表之间关系: FK  OneToOne  ManyToMany关系,而contenttype就可以混搭,可牛鼻了。

    表设计:

     1 from django.db import models
     2 from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
     3 from django.contrib.contenttypes.models import ContentType
     4 
     5 
     6 class Course(models.Model):
     7     """
     8     大一基础课
     9     """
    10     title = models.CharField(max_length=32)
    11     # 仅用于反向查找
    12     price_policy_list = GenericRelation('PricePolicy')
    13 
    14 
    15 class DegreeCourse(models.Model):
    16     """
    17     大三学位课
    18     """
    19     title = models.CharField(max_length=32)
    20 
    21 
    22 class PricePolicy(models.Model):
    23     """
    24     老师价格策略
    25     """
    26     price = models.IntegerField()
    27     period = models.IntegerField()
    28 
    29     # table_name = models.CharField(verbose_name='关联表的名称')
    30     # object_id = models.CharField(verbose_name='关联表中的数据行id')
    31     content_type = models.ForeignKey(ContentType, verbose_name='关联大一基础课和大三学位课', on_delete=models.CASCADE)
    32     object_id = models.IntegerField(verbose_name='关联大一基础课和大三学位课中的课程id')
    33 
    34     content_object = GenericForeignKey('content_type', 'object_id')
    35 
    36 """
    37 obj = models.DegreeCourse.objects.filter(title='单片机').first()
    38 # obj.id
    39 cobj = models.ContentType.objects.filter(model='Course').first()
    40 # cobj.id
    41 models.PricePolicy.objects.create(price=5.9, period=40, content_type_id=cobj.id, object_id=obj.id)
    42 
    43 
    44 obj = models.DegreeCourse.objects.filter(title='单片机').first()
    45 models.PricePolicy.objects.create(price=5.9, period=40, content_object=obj)
    46 """

    from django.contrib import admin
    from django.urls import path
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('test/', views.test),
    ]
    
     1 from django.shortcuts import render, HttpResponse
     2 from app01 import models
     3 
     4 
     5 def test(request):
     6     # 1.为大三学位课“单片机”添加一个价格策略,40分钟 5.9
     7     # obj = models.DegreeCourse.objects.filter(title='单片机').first()
     8     # obj.id
     9     # cobj = models.ContentType.objects.filter(model='Course').first()
    10     # cobj.id
    11     # models.PricePolicy.objects.create(price=5.9, period=40, content_type_id=cobj.id, object_id=obj.id)
    12 
    13     # obj = models.DegreeCourse.objects.filter(title='单片机').first()
    14     # models.PricePolicy.objects.create(price=5.9, period=40, content_object=obj)
    15     # 2.根据课的id获取课程并获得课程的所有价格策略
    16     course = models.Course.objects.filter(id=1).first()
    17     price_policys = course.price_policy_list.all()
    18     print(price_policys)
    19     return HttpResponse('.......')

     总结:

      1.所有关联数据库的表,不用写表名,我们也不用录表名,通过FK获取关联就是了。

      2.帮助我们快速插入数据用了GenericForeignKey。

      3.加上GenericRelation反向快速查找。

      

  • 相关阅读:
    简单工厂模式
    c# 接口属性继承
    web安全测试---跨站点脚本测试
    web安全测试---WebScarab工具介绍
    Appscan安全漏洞扫描使用(转)
    web安全测试---AppScan扫描工具(转)
    修改DB-LINK连接数方法
    LoadRunner 11破解方法
    数据库学习网站和linux学习网站
    关于误删表可在回收站中闪回
  • 原文地址:https://www.cnblogs.com/Alexephor/p/11309475.html
Copyright © 2020-2023  润新知