Django ContentType组件

ContentType组件

引入

现在我们有这样一个需求~我们的商城里有很多的商品~~节日要来了~我们要搞活动~~

那么我们就要设计优惠券~~优惠券都有什么类型呢~~满减的~折扣的~立减的~~

我们对应着我们活动类型~对我们的某类商品设计优惠券~~比如~~

家电是一类商品~~食物是一类商品~那么我们可以设计家电折扣优惠券~~以及食物满减优惠券等~

那么我们看表结构怎么设计

django.db <span style="color: #0000ff;">class<span style="color: #000000;"> Appliance(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
家用电器表
id name
1 冰箱
2 电视
3 洗衣机
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=64<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Food(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
食物表
id name
1 面包
2 牛奶
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=32<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Fruit(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
水果表
id name
1 苹果
2 香蕉
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=32<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Coupon(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
优惠券表
id name appliance_id food_id fruit_id
1 通用优惠券 null null null
2 冰箱折扣券 1 null null
3 电视折扣券 2 null null
4 苹果满减卷 null null 1
我每增加一张表就要多增加一个字段
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=32<span style="color: #000000;">)
appliance = models.ForeignKey(to=<span style="color: #800000;">"<span style="color: #800000;">Appliance<span style="color: #800000;">",null=True,blank=<span style="color: #000000;">True)
food = models.ForeignKey(to=<span style="color: #800000;">"<span style="color: #800000;">Food<span style="color: #800000;">",blank=<span style="color: #000000;">True)
fruit = models.ForeignKey(to=<span style="color: #800000;">"<span style="color: #800000;">Fruit<span style="color: #800000;">",blank=True)
<span style="color: #008000;">#<span style="color: #008000;"> 实际上我们商品的种类会特别的多,导致我们这张表外键越来越多

遇到这种一张表要跟多张表进行外键关联的时候~我们Django提供了ContentType组件~

ContentType组件

ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中。

当我们的项目做数据迁移后,会有很多django自带的表,其中就有django_content_type表,我们可以去看下

ContentType组件应用:

  -- 在model中定义ForeignKey字段,并关联到ContentType表,通常这个字段命名为content-type

  -- 在model中定义PositiveIntergerField字段,用来存储关联表中的主键,通常我们用object_id

  -- 在model中定义GenericForeignKey字段,传入上面两个字段的名字

  --  方便反向查询可以定义GenericRelation字段

代码如下:

django.db django.contrib.contenttypes.models django.contrib.contenttypes.fields <span style="color: #0000ff;">class<span style="color: #000000;"> Appliance(models.Model):
<span style="color: #800000;">"""
<span style="color: #800000;">
家用电器表
id name
1 冰箱
2 电视
3 洗衣机
<span style="color: #800000;">"""
<span style="color: #000000;">
name
= models.CharField(max_length=64<span style="color: #000000;">)
coupons
= GenericRelation(to=<span style="color: #800000;">"
<span style="color: #800000;">Coupon<span style="color: #800000;">"<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Food(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
食物表
id name
1 面包
2 牛奶
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=32<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Fruit(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
水果表
id name
1 苹果
2 香蕉
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=32<span style="color: #000000;">)

<span style="color: #0000ff;">class<span style="color: #000000;"> Coupon(models.Model):
<span style="color: #800000;">"""<span style="color: #800000;">
优惠券表
id name appliance_id food_id fruit_id
1 通用优惠券 null null null
2 冰箱折扣券 1 null null
3 电视折扣券 2 null null
4 苹果满减卷 null null 1
我每增加一张表就要多增加一个字段
<span style="color: #800000;">"""<span style="color: #000000;">
name = models.CharField(max_length=32<span style="color: #000000;">)
<span style="color: #008000;">#<span style="color: #008000;"> appliance = models.ForeignKey(to="Appliance",blank=True)
<span style="color: #008000;">#<span style="color: #008000;"> food = models.ForeignKey(to="Food",blank=True)
<span style="color: #008000;">#<span style="color: #008000;"> fruit = models.ForeignKey(to="Fruit",blank=True)
<span style="color: #008000;">#<span style="color: #008000;"> 第一步
content_type = models.ForeignKey(to=<span style="color: #000000;">ContentType)
<span style="color: #008000;">#<span style="color: #008000;"> 第二步
object_id =<span style="color: #000000;"> models.PositiveIntegerField()
<span style="color: #008000;">#<span style="color: #008000;"> 第三步
content_object = GenericForeignKey(<span style="color: #800000;">'<span style="color: #800000;">content_type<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">object_id<span style="color: #800000;">'<span style="color: #000000;">)  
数据迁移后~添加数据~我们看下增删改查的操作~~<span style="color: #000000;">

基本的使用~

<span style="color: #0000ff;">from django.http <span style="color: #0000ff;">import<span style="color: #000000;"> HttpResponse
<span style="color: #0000ff;">from rest_framework.views <span style="color: #0000ff;">import<span style="color: #000000;"> APIView
<span style="color: #0000ff;">from rest_framework.response <span style="color: #0000ff;">import<span style="color: #000000;"> Response
<span style="color: #0000ff;">from django.contrib.contenttypes.models <span style="color: #0000ff;">import<span style="color: #000000;"> ContentType
<span style="color: #0000ff;">from .models <span style="color: #0000ff;">import<span style="color: #000000;"> Appliance,Coupon

<span style="color: #008000;">#<span style="color: #008000;"> Create your views here.

<span style="color: #0000ff;">class<span style="color: #000000;"> Test(APIView):

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; get(self,request):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 通过ContentType获得表名</span>
    content = ContentType.objects.filter(app_label=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;app01</span><span style="color: #800000;"&gt;"</span>,model=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;appliance</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;).first()
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 获得表model对象 相当于models.Applicance</span>
    model_class =<span style="color: #000000;"&gt; content.model_class()
    ret </span>=<span style="color: #000000;"&gt; model_class.objects.all()

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 为海尔冰箱创建一条优惠记录</span>
    ice_box = Appliance.objects.filter(id=1<span style="color: #000000;"&gt;).first()
    Coupon.objects.create(name</span>=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;海尔冰箱折扣券</span><span style="color: #800000;"&gt;"</span>,content_object=<span style="color: #000000;"&gt;ice_box)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 查询优惠券id=1绑定了哪个商品</span>
    coupon_obj = Coupon.objects.filter(id=1<span style="color: #000000;"&gt;).first()
    goods_obj </span>=<span style="color: #000000;"&gt; coupon_obj.content_object
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(goods_obj.name)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 查询海尔冰箱的所有优惠券 id=1</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 我们定义了反向查询</span>
    results =<span style="color: #000000;"&gt; ice_box.coupons.all()
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(results[0].name)

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 如果没定义反向查询</span>
    content = ContentType.objects.filter(app_label=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;app01</span><span style="color: #800000;"&gt;"</span>,model=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;appliance</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;).first()
    result </span>= Coupon.objects.filter(content_type=content,object_id=1<span style="color: #000000;"&gt;).all()
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(result[0].name)
    </span><span style="color: #0000ff;"&gt;return</span> HttpResponse(ret)</pre>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


注:所有源代码均实测运行过。所有源代码均已上传CSDN,请有需要的朋友自行下载。
继承APIView和ViewSetMixin;作用也与APIView基本类似,提供了身份认证、权限校验、流量管理等。ViewSet在开发接口中不经常用。
一、Django介绍Python下有许多款不同的 Web 框架。Django是重量级选手中最有代表性的一位。许多成功的网站和APP都基于Django。Django 是一个开放源代码的 Web 应用框架,由 Python 写成。Django 遵守 BSD 版权,初次发布于 2005 年 7 月, 并于 2008 年 9 月发布了第一个正式版本 1.0 。Django学习线路Django 采用了 MVT 的软件设计模式,即模型(Model),视图(View)和模板(Template)。这个MVT模式并
本文从nginx快速掌握到使用,gunicorn快速掌握到使用,实现小白快速搭建django项目,并对可能出现的报错进行了分析
uniapp微信小程序订阅消息发送服务通知
Django终端打印SQL语句 1 Setting配置: 2 默认python 使用的MysqlDB连接,Python3 支持支持pymysql 所有需要在app里面的__init__加上下面配置:
url: re_path(&#39;authors/$&#39;, views.AuthorView.as_view()), re_path(&#39;book/(?P\d+)/$&#39;, vie
前提 关于html寻找路线: template 如果在各个APP中存在, Django 会优先找全局template 文件下的html文件,如果全局下的template文件没有相关的html Djan
// GET请求request.GET // POST请求request.POST // 处理文件上传请求request.FILES // 处理如checkbox等多选 接受列表request.get
from bs4 import BeautifulSoup#kindeditordef kindeditor(request): s = &#39;&#39;&#39; &lt;li&gt;&lt;s
view.py 配置 html 配置
from django.http import JsonResponse JsonResponse 里面代码会加这一个响应头 kwargs.setdefault(&#39;content_type&#
#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) &gt
return HttpResponse(&quot;OK&quot;) 返回一个字符串 return redirect(&quot;/index/&quot;) 返回URL return render
from django.http import JsonResponse JsonResponse 里面代码会加这一个响应头 kwargs.setdefault(&#39;content_type&#
浏览器有一个很重要的概念——同源策略(Same-Origin Policy)。所谓同源是指,域名,协议,端口相同。不同源的客户端脚本(javascript、ActionScript)在没明确授权的情况
自动发送 &gt; 依赖jQuery文件 实例--&gt;GET请求: 手动发送 &gt; 依赖浏览器XML对象(也叫原生ajax) Ajax主要就是使用 【XmlHttpRequest】对象来完成请
#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) &gt
// GET请求request.GET // POST请求request.POST // 处理文件上传请求request.FILES // 处理如checkbox等多选 接受列表request.get
return HttpResponse(&quot;OK&quot;) 返回一个字符串 return redirect(&quot;/index/&quot;) 返回URL return render