欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

content-type組件怎么在Django中使用-創(chuàng)新互聯(lián)

content-type組件怎么在Django中使用?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

目前累計服務客戶成百上千家,積累了豐富的產(chǎn)品開發(fā)及服務經(jīng)驗。以網(wǎng)站設計水平和技術實力,樹立企業(yè)形象,為客戶提供成都網(wǎng)站設計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設、網(wǎng)站策劃、網(wǎng)頁設計、網(wǎng)絡營銷、VI設計、網(wǎng)站改版、漏洞修補等服務。創(chuàng)新互聯(lián)建站始終以務實、誠信為根本,不斷創(chuàng)新和提高建站品質(zhì),通過對領先技術的掌握、對創(chuàng)意設計的研究、對客戶形象的視覺傳遞、對應用系統(tǒng)的結(jié)合,為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。

django中的一個組件content-type可以幫助我們解決這樣的一個問題

在這里我先設計了3張表 學位表 普通課程 和價格策略表 大致的設計如下

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

在上圖中我們可以看到價格策略表和其他的兩個表進行了關聯(lián),可以根據(jù)表明

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)
  # 僅用于反向查找 不在數(shù)據(jù)庫中添加字段
  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()

  # 關聯(lián)表
  content_type = models.ForeignKey(ContentType, verbose_name='關聯(lián)的表名稱') # 7,8 表名稱
  object_id = models.IntegerField(verbose_name='關聯(lián)的表中的數(shù)據(jù)行的ID')  #
  # 幫助你快速實現(xiàn)content_type操作 ,快速插入數(shù)據(jù) 不生成數(shù)據(jù)庫中的字段 
  content_object = GenericForeignKey('content_type', 'object_id')

進行插入數(shù)據(jù)的類視圖

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. 根據(jù)課程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),
]

我們自己進行插入數(shù)據(jù)可能會這樣寫

# 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

數(shù)據(jù)庫中的結(jié)果如下

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

看完上述內(nèi)容,你們掌握content-type組件怎么在Django中使用的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站名稱:content-type組件怎么在Django中使用-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://www.chinadenli.net/article14/iiede.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)網(wǎng)站建設網(wǎng)站設計公司品牌網(wǎng)站建設手機網(wǎng)站建設移動網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設