Django包含一個contenttypes應用程序(app),能夠跟蹤Django項目中安裝的全部模型(Model),提供用於處理模型的高級通用接口。數據庫
生成表結構以後有一個表,包含全部其餘表django
該組件主要應用於像不一樣的帖子和不一樣的照片都有評論,可是想只用一張評論表去存儲,評論表中應有一個字段說明屬於帖子仍是照片,有一個字段說明該評論屬於具體哪一個帖子、哪一個照片。app
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')
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('...')
"""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), ]