django ContentType使用方法

content_type表将app名称与其中的表的关系进行保存

models.py

class Test1(models.Model):
name = models.CharField(max_length=100)
#反向查找
price = GenericRelation('Test3')

class Test2(models.Model):
name = models.CharField(max_length=100)
price = GenericRelation('Test3')

class Test3(models.Model):
price = models.CharField(max_length=100)
contex_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
obj_id = models.IntegerField()
#帮助contentType操作
contex_tobj = GenericForeignKey('contex_type','obj_id')

views.py

obj = Test1.objects.get(id=1)
Test3.objects.create(price=10,contex_tobj=obj)

 

转载于:https://www.cnblogs.com/SealLiu/p/10254827.html

你可能感兴趣的:(django ContentType使用方法)