2006-06-17 03:18:30 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Generic relations
|
2006-06-17 03:18:30 +08:00
|
|
|
|
|
|
|
Generic relations let an object have a foreign key to any object through a
|
2008-08-12 22:15:38 +08:00
|
|
|
content-type/object-id field. A ``GenericForeignKey`` field can point to any
|
|
|
|
object, be it animal, vegetable, or mineral.
|
2006-06-17 03:18:30 +08:00
|
|
|
|
2006-06-20 11:03:43 +08:00
|
|
|
The canonical example is tags (although this example implementation is *far*
|
2006-06-17 03:18:30 +08:00
|
|
|
from complete).
|
|
|
|
"""
|
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2010-10-15 14:52:35 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.db import models
|
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
|
|
|
|
class TaggedItem(models.Model):
|
|
|
|
"""A tag on an item."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
tag = models.SlugField()
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2006-06-17 03:18:30 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
class Meta:
|
2015-01-08 23:58:23 +08:00
|
|
|
ordering = ["tag", "content_type__model"]
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.tag
|
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2008-09-02 23:26:00 +08:00
|
|
|
class ValuableTaggedItem(TaggedItem):
|
|
|
|
value = models.PositiveIntegerField()
|
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
|
|
|
class AbstractComparison(models.Model):
|
2008-08-01 23:54:53 +08:00
|
|
|
comparative = models.CharField(max_length=50)
|
2008-08-12 22:15:38 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type1 = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="comparative1_set"
|
|
|
|
)
|
2008-08-01 23:54:53 +08:00
|
|
|
object_id1 = models.PositiveIntegerField()
|
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
first_obj = GenericForeignKey(ct_field="content_type1", fk_field="object_id1")
|
2013-10-04 01:44:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Comparison(AbstractComparison):
|
|
|
|
"""
|
|
|
|
A model that tests having multiple GenericForeignKeys. One is defined
|
|
|
|
through an inherited abstract model and one defined directly on this class.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type2 = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="comparative2_set"
|
|
|
|
)
|
2008-08-01 23:54:53 +08:00
|
|
|
object_id2 = models.PositiveIntegerField()
|
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
other_obj = GenericForeignKey(ct_field="content_type2", fk_field="object_id2")
|
2008-08-01 23:54:53 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%s is %s than %s" % (self.first_obj, self.comparative, self.other_obj)
|
2008-08-01 23:54:53 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
class Animal(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
common_name = models.CharField(max_length=150)
|
|
|
|
latin_name = models.CharField(max_length=150)
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2014-03-04 19:23:32 +08:00
|
|
|
tags = GenericRelation(TaggedItem, related_query_name="animal")
|
2014-01-22 14:43:33 +08:00
|
|
|
comparisons = GenericRelation(
|
|
|
|
Comparison, object_id_field="object_id1", content_type_field="content_type1"
|
|
|
|
)
|
2006-06-17 03:18:30 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.common_name
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
class Vegetable(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=150)
|
2006-06-17 03:18:30 +08:00
|
|
|
is_yucky = models.BooleanField(default=True)
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
tags = GenericRelation(TaggedItem)
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.name
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2015-10-08 21:15:28 +08:00
|
|
|
class Carrot(Vegetable):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
class Mineral(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=150)
|
2006-06-17 03:18:30 +08:00
|
|
|
hardness = models.PositiveSmallIntegerField()
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
# note the lack of an explicit GenericRelation here...
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.name
|
2011-05-22 23:21:03 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2011-05-22 23:21:03 +08:00
|
|
|
class GeckoManager(models.Manager):
|
2013-03-08 22:15:23 +08:00
|
|
|
def get_queryset(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_queryset().filter(has_tail=True)
|
2011-05-22 23:21:03 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2011-05-22 23:21:03 +08:00
|
|
|
class Gecko(models.Model):
|
2013-08-12 04:19:09 +08:00
|
|
|
has_tail = models.BooleanField(default=False)
|
2011-05-22 23:21:03 +08:00
|
|
|
objects = GeckoManager()
|
2013-02-02 08:56:27 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2013-02-02 08:56:27 +08:00
|
|
|
# To test fix for #11263
|
|
|
|
class Rock(Mineral):
|
2014-01-22 14:43:33 +08:00
|
|
|
tags = GenericRelation(TaggedItem)
|
2013-05-11 08:48:58 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2015-10-03 02:28:15 +08:00
|
|
|
class ValuableRock(Mineral):
|
|
|
|
tags = GenericRelation(ValuableTaggedItem)
|
|
|
|
|
|
|
|
|
2013-05-11 08:48:58 +08:00
|
|
|
class ManualPK(models.Model):
|
|
|
|
id = models.IntegerField(primary_key=True)
|
2014-03-04 19:23:32 +08:00
|
|
|
tags = GenericRelation(TaggedItem, related_query_name="manualpk")
|
2013-05-21 01:51:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ForProxyModelModel(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2013-05-21 01:51:05 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
obj = GenericForeignKey(for_concrete_model=False)
|
2013-05-21 01:51:05 +08:00
|
|
|
title = models.CharField(max_length=255, null=True)
|
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2013-05-21 01:51:05 +08:00
|
|
|
class ForConcreteModelModel(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2013-05-21 01:51:05 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
obj = GenericForeignKey()
|
2013-05-21 01:51:05 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2013-05-21 01:51:05 +08:00
|
|
|
class ConcreteRelatedModel(models.Model):
|
2014-01-22 14:43:33 +08:00
|
|
|
bases = GenericRelation(ForProxyModelModel, for_concrete_model=False)
|
2013-05-21 01:51:05 +08:00
|
|
|
|
2013-10-04 01:44:10 +08:00
|
|
|
|
2013-05-21 01:51:05 +08:00
|
|
|
class ProxyRelatedModel(ConcreteRelatedModel):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
2013-10-15 23:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
# To test fix for #7551
|
|
|
|
class AllowsNullGFK(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.SET_NULL, null=True)
|
2013-10-15 23:24:35 +08:00
|
|
|
object_id = models.PositiveIntegerField(null=True)
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|