网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

content-type组件怎么在Django中使用-创新互联

content-type组件怎么在Django中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

目前累计服务客户成百上千家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供成都网站设计、成都做网站、外贸网站建设、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。创新互联建站始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。

django中的一个组件content-type可以帮助我们解决这样的一个问题

在这里我先设计了3张表 学位表 普通课程 和价格策略表 大致的设计如下

content-type组件怎么在Django中使用

在上图中我们可以看到价格策略表和其他的两个表进行了关联,可以根据表明

models.py

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


class Course(models.Model):
  """
  普通课程
  """
  title = models.CharField(max_length=32)
  # 仅用于反向查找 不在数据库中添加字段
  price_policy_list = GenericRelation("PricePolicy")

class DegreeCourse(models.Model):
  """
  学位课程
  """
  title = models.CharField(max_length=32)

  # 仅用于反向查找
  price_policy_list = GenericRelation("PricePolicy")


class PricePolicy(models.Model):
  """
  价格策略
  """
  price = models.IntegerField()
  period = models.IntegerField()

  # 关联表
  content_type = models.ForeignKey(ContentType, verbose_name='关联的表名称') # 7,8 表名称
  object_id = models.IntegerField(verbose_name='关联的表中的数据行的ID')  #
  # 帮助你快速实现content_type操作 ,快速插入数据 不生成数据库中的字段 
  content_object = GenericForeignKey('content_type', 'object_id')

进行插入数据的类视图

from django.shortcuts import render,HttpResponse
from app01 import models
def test(request):

  # 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9
  # obj1 = models.DegreeCourse.objects.filter(title='Python全栈').first()
  # models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
  #
  # obj2 = models.DegreeCourse.objects.filter(title='Python全栈').first()
  # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
  #
  # obj3 = models.DegreeCourse.objects.filter(title='Python全栈').first()
  # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)

  # 2. 为学位课“rest”添加一个价格策略:一个月 9.9
  # obj1 = models.Course.objects.filter(title='rest framework').first()
  # models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
  #
  # obj2 = models.Course.objects.filter(title='rest framework').first()
  # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
  #
  # obj3 = models.Course.objects.filter(title='rest framework').first()
  # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)

  # 3. 根据课程ID获取课程, 并获取该课程的所有价格策略
  # course = models.Course.objects.filter(id=1).first()
  #
  # price_policys = course.price_policy_list.all()
  #
  # print(price_policys)


  return HttpResponse('...')

为其添加路由

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^test/', views.test),
]

我们自己进行插入数据可能会这样写

# 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9
"""
obj = DegreeCourse.objects.filter(title='Python全栈').first()
# obj.id
cobj = ContentType.objects.filter(model='course').first()
# cobj.id
PricePolicy.objects.create(price='9.9',period='30',content_type_id=cobj.id,object_id=obj.id)
"""
# obj = DegreeCourse.objects.filter(title='Python全栈').first()
# PricePolicy.objects.create(price='9.9',period='30',content_object=obj)

输入以下的地址进行测试

http://127.0.0.1:8000/test

数据库中的结果如下

content-type组件怎么在Django中使用

看完上述内容,你们掌握content-type组件怎么在Django中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


当前标题:content-type组件怎么在Django中使用-创新互联
文章位置:http://bjjierui.cn/article/iiede.html

其他资讯