1. 程式人生 > >Django contenttypes組件

Django contenttypes組件

req () title splay cti 字段說明 field urn ati

Django包含一個contenttypes應用程序(app),可以跟蹤Django項目中安裝的所有模型(Model),提供用於處理模型的高級通用接口。

生成表結構之後有一個表,包含所有其他表

技術分享圖片

技術分享圖片

該組件主要應用於像不同的帖子和不同的照片都有評論,但是想只用一張評論表去存儲,評論表中應有一個字段說明屬於帖子還是照片,有一個字段說明該評論屬於具體哪個帖子、哪個照片。

技術分享圖片
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation,ContentType
class Post(models.Model): """帖子表""" title = models.CharField(max_length=72) # 查看一個對象的全部評論用comments,創建時用GenericRelation comments=GenericRelation(Comment) class Picture(models.Model): """圖片表""" image = models.ImageField() comments = GenericRelation(Comment) class
Comment(models.Model): """評論表""" content = models.TextField() # post = models.ForeignKey(Post, null=True, blank=True, on_delete=models.CASCADE) # picture = models.ForeignKey(Picture, null=True, blank=True, on_delete=models.CASCADE) # 外鍵關聯ContentType(其中有所有的表) content_type=models.ForeignKey(ContentType,on_delete=models.DO_NOTHING)
# 關聯數據的主鍵,具體某一行的id(例如具體某一個帖子的id) object_id=models.PositiveSmallIntegerField() # GenericForeignKey字段創建,在數據庫中不會存在該字段 content_object=GenericForeignKey(content_type,object_id)
models.py 技術分享圖片
from django.shortcuts import render,HttpResponse
from app01 import models
from django.contrib.contenttypes.fields import ContentType
# Create your views here.
def Test(request):
    # 創建測試數據 方式一
    # content_type_pic_obj=ContentType.objects.filter(model=‘picture‘).first()
    # picture_obj=models.Picture.objects.filter(id=1).first()
    # models.Comment.objects.create(content=‘圖片好看2‘,content_type=content_type_pic_obj,object_id=picture_obj.id)
    # models.Comment.objects.create(content=‘圖片好看3‘,content_type=content_type_pic_obj,object_id=picture_obj.id)
    # models.Comment.objects.create(content=‘圖片好看4‘,content_type=content_type_pic_obj,object_id=picture_obj.id)
    # content_type_post_obj = ContentType.objects.filter(model=‘post‘).first()
    # post_obj_1 = models.Post.objects.filter(title=‘散文1‘).first()
    # post_obj_2 = models.Post.objects.filter(title=‘散文2‘).first()
    # models.Comment.objects.create(content=‘散文寫的好‘, content_type=content_type_post_obj, object_id=post_obj_1.id)
    # models.Comment.objects.create(content=‘散文寫的好2‘, content_type=content_type_post_obj, object_id=post_obj_1.id)
    # models.Comment.objects.create(content=‘散文寫的好3‘, content_type=content_type_post_obj, object_id=post_obj_2.id)

    # 創建測試數據方式二
    # picture_obj = models.Picture.objects.filter(image=‘default_avatar.jpg‘).first()
    # models.Comment.objects.create(content_object=picture_obj,content=‘圖片一般般‘)
    # post_obj_1 = models.Post.objects.filter(title=‘散文1‘).first()
    # models.Comment.objects.create(content_object=post_obj_1, content=‘文章一般般‘)

    # 查詢一個對象的所有評論
    comment_list=models.Post.objects.filter(id=1).first().comments.all()
    print(comment_list)
    return HttpResponse(...)
views.py 技術分享圖片
"""django_contenttypes編寫models URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path(‘‘, views.home, name=‘home‘)
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path(‘‘, Home.as_view(), name=‘home‘)
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path(‘blog/‘, include(‘blog.urls‘))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
    path(admin/, admin.site.urls),
    path(test/, views.Test),
]
urls.py

Django contenttypes組件