标签:class 反向 port let obj gen 关联表 types har
使用方式:
- 导入模块
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")
标签:class 反向 port let obj gen 关联表 types har
原文地址:https://www.cnblogs.com/duhong0520/p/11872798.html