标签:记录 include img mod play import max HERE 第一步
ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中。
当项目做数据迁移后,会有很多django自带的表,其中就有django_content_type表
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 Food(models.Model): """ id title 1 面包 2 牛奶 """ title = models.CharField(max_length=32) # 不会生成字段 只用于反向查询 coupons = GenericRelation(to="Coupon") class Fruit(models.Model): """ id title 1 苹果 2 香蕉 """ title = models.CharField(max_length=32) # 如果有40张表 # class Coupon(models.Model): # """ # id title food_id fruit_id # 1 面包九五折 1 null # 2 香蕉满10元减5元 null 2 # """ # title = models.CharField(max_length=32) # food = models.ForeignKey(to="Food") # fruit = models.ForeignKey(to="Fruit") # class Coupon(models.Model): # """ # id title table_id object_id # 1 面包九五折 1 1 # 2 香蕉满10元减5元 2 2 # """ # title = models.CharField(max_length=32) # table = models.ForeignKey(to="Table") # object_id = models.IntegerField() # # # class Table(models.Model): # """ # id app_name table_name # 1 demo food # 2 demo fruit # """ # app_name = models.CharField(max_length=32) # table_name = models.CharField(max_length=32) class Coupon(models.Model): title = models.CharField(max_length=32) # 第一步 content_type = models.ForeignKey(to=ContentType, on_delete=None) # 第二步 object_id = models.IntegerField() # 第三步 不会生成字段 content_object = GenericForeignKey("content_type", "object_id")
from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from .models import Food, Coupon from django.contrib.contenttypes.models import ContentType # Create your views here. class DemoView(APIView): def get(self, request): # 给面包创建一个优惠券 food_obj = Food.objects.filter(id=1).first() # Coupon.objects.create(title="面包九五折", content_type_id=8, object_id=1) # Coupon.objects.create(title="双十一面包九折促销", content_object=food_obj) #查询面包都有哪些优惠券 #定义了反向查询 coupons = food_obj.coupons.all() print(coupons) # 如果没定义反向查询 content = ContentType.objects.filter(app_label="app01", model="food").first() coupons = Coupon.objects.filter(content_type=content, object_id=1).all() print(coupons) # 优惠券查对象 # 查询优惠券id=1绑定了哪个商品 coupon_obj = Coupon.objects.filter(id=1).first() content_obj = coupon_obj.content_object print(coupon_obj.title,content_obj.title) # 通过ContentType表找表模型 content = ContentType.objects.filter(app_label="app01", model="food").first() # 获得表model对象 相当于models.Applicance model_class = content.model_class() ret = model_class.objects.all() print(ret) return Response("ContentType测试")
#静态文件 STATIC_URL = ‘/static/‘ STATICFILES_DIRS=( os.path.join(BASE_DIR,‘static‘), ) # Media配置 MEDIA_URL = "media/" MEDIA_ROOT = os.path.join(BASE_DIR, "media")
from django.conf.urls import url, include from django.contrib import admin from django.views.static import serve from new_luffy import settings urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^api/course/‘, include("course.urls")), # media路径配置 url(r‘media/(?P<path>.*)$‘, serve, {‘document_root‘: settings.MEDIA_ROOT}) ]
标签:记录 include img mod play import max HERE 第一步
原文地址:https://www.cnblogs.com/bubu99/p/10493619.html