• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MC_Hotdog
Yeah, you're right I'm still riding that crappy bike
博客园    首页    新随笔    联系   管理    订阅  订阅

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反向快速查找。

  

 

posted @ 2019-08-07 21:42  MC_Hotdog  阅读(220)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3