views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Food, Coupon
from django.contrib.contenttypes.models import ContentType
class DemoView(APIView):
    def get(self, request):
        # 1. 找到具体食物对象
        food_obj = Food.objects.filter(id=1).first()
        # 2. 创建食物优惠券:手动实现(不推荐)
        # Coupon.objects.create(title="面包九五折", content_type_id=8, object_id=1)
        # 2. 创建食物优惠券:Django内置方法实现
        Coupon.objects.create(title="面包九五折", content_object=food_obj)
        # 3.1 通过食物对象查询优惠券对象(反向查询GenericRelation)
        coupons = food_obj.coupons.all()
        # 3.2 通过优惠券对象查询食物对象(正向查询content_object)
        coupon_obj = Coupon.objects.filter(id=1).first()
        content_obj = coupon_obj.content_object
        # 4. 通过ContentType表找到表模型:app名 + 表名
        content = ContentType.objects.filter(app_label="demo", model="food").first()
        print(content)  # food
        # model_class()方法得到食物表模型,即food_obj
        model_class = content.model_class()
        ret = model_class.objects.all()
        print(ret)  # <QuerySet [..., ...]>
        return Response("ContentType测试")