contentType

使用方式:
- 导入模块
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

- 定义models
class PythonBasic(models.Model):
course_name = models.CharField(max_length=32)
# 定义反向查询字段
coupons = GenericRelation(to="Coupon")


class Oop(models.Model):
course_name = models.CharField(max_length=32)


class Coupon(models.Model):
coupon_name = models.CharField(max_length=32)
# 关联到ContentType的外键
content_type = models.Foreignkey(ContentType, on_delete=models.CASCADE)
# 关联表的主键id
object_id = models.PositiveIntegerField()

content_object = GenericForeignKey("content_type", "object_id")

posted on 2019-11-16 18:12  Adudu001  阅读(240)  评论(0)    收藏  举报

导航