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