美文网首页
ContentType组件

ContentType组件

作者: aq_wzj | 来源:发表于2018-12-20 09:30 被阅读0次
image.png image.png

如图, 每一个课程类型对应一张表, 每一门课程都有不同的价格周期对应的价格, 所以价格表会有一个表名字段对应到ContentType表的pk, 但是这样每给一门课程加上价格策略都要有如下步骤

  1. 拿到课程id

  2. 拿到表名, 通过表名拿到ContentType表对应的id

  3. 往价格表里面添加数据

通过课程拿到价格

  1. 拿到表名

  2. 通过ContentType表拿到对应的表名id

  3. 在价格表通过表名id与课程id拿到价格

很麻烦!!!!

Django给我们提供了ContentType组件, 自带ContentType这个表

用法:

模型层:

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

# Create your models here.
# 学位课
class Course1(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    # 通过课程拿到价格时用的, 参数一:价格表,参数与参数三都为价格表的字段
    policy = GenericRelation('PricePolicy', object_id_field='object_id', content_type_field='contentType')


# 专题课
class Course2(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    policy = GenericRelation('PricePolicy', object_id_field='object_id', content_type_field='contentType')

# 轻客    
class Course3(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    policy = GenericRelation('PricePolicy', object_id_field='object_id', content_type_field='contentType')

# 价格表
class PricePolicy(models.Model):
    # 跟ContentType表做外键关联
    contentType = models.ForeignKey(to=ContentType)
    # 正整数,课程id
    object_id = models.PositiveIntegerField()
    content_obj = GenericForeignKey('contentType', 'object_id')
    
    
    period = models.CharField(max_length=32)
    price = models.FloatField()

视图层:

1. 存价格
def test(request):
    course = models.Course1.objects.filter(pk=1).first()
    # 只需传入课程对象,周期,价格,就会自动存入表id与课程id
    models.PricePolicy.objects.create(
        period='60', price=800, content_obj=course
    )
    return HttpResponse('ok')

2. 询所有价格策略,并且显示对应的课程名称
def test(request):
    ret=models.PricePolicy.objects.all()
    for i in ret:
        print(i.price)
        print(i.period)
        # content_obj 就是代指关联的课程,或者学位课程的那个对象
        print(type(i.content_obj))
        print(i.content_obj.name)
    return HttpResponse('ok')

3. 通过课程id,获取课程信息和价格策略
def test(request):
    course=models.Course1 .objects.filter(pk=1).first()
    print(course.policy.all())
    return HttpResponse('ok')
    

相关文章

  • ContentType组件

    如图, 每一个课程类型对应一张表, 每一门课程都有不同的价格周期对应的价格, 所以价格表会有一个表名字段对应到Co...

  • Django:rest framework补充ContentTy

    ContentType django内置的ContentType组件就是帮我们做连表操作如果一个表与其他表有多个外...

  • 32_Django ContentType组件

    ContentType组件 一、简单了解 二、简单使用 models.py 文件: views.py 文件:

  • Content-type类型记录

    {contentType.put(".load" , "text/html");contentType.put("...

  • FastApi Post请求

    Json数据 contentType:application/json 表单数据(键值对) contentType...

  • ContentType

    application/x-www-form-urlencoded form数据被编码为名称/值对。这是标准的编码...

  • ContentType

    概念 Content-Type,内容类型,一般是指网页中存在的Content-Type,用于定义网络文件的类型和网...

  • 关于postman中ContentType与js写法的对应关系

    ContentType的常用类型的有以下两种: "ContentType":"application/json" ...

  • Django的ContenType组件

    ContentType是Django的内置组件,在执行迁移的时候会自动生成,用于将一个表与多个表进行连接。 假设现...

  • 文件下载示例代码(JAVA)

    后台代码 知识拓展 response的contentType的类型值 ContentType 属性指定服务器响应的...

网友评论

      本文标题:ContentType组件

      本文链接:https://www.haomeiwen.com/subject/zomekqtx.html