2006-06-17 03:18:30 +08:00
|
|
|
"""
|
2007-03-24 04:17:04 +08:00
|
|
|
34. 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).
|
|
|
|
"""
|
|
|
|
|
2007-05-08 18:59:35 +08:00
|
|
|
from django.contrib.contenttypes import generic
|
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."""
|
|
|
|
tag = models.SlugField()
|
|
|
|
content_type = models.ForeignKey(ContentType)
|
|
|
|
object_id = models.PositiveIntegerField()
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2007-05-08 18:59:35 +08:00
|
|
|
content_object = generic.GenericForeignKey()
|
2007-12-09 15:12:07 +08:00
|
|
|
|
2006-06-17 03:18:30 +08:00
|
|
|
class Meta:
|
2010-10-17 09:49:36 +08:00
|
|
|
ordering = ["tag", "content_type__name"]
|
2007-12-09 15:12:07 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.tag
|
|
|
|
|
2008-09-02 23:26:00 +08:00
|
|
|
class ValuableTaggedItem(TaggedItem):
|
|
|
|
value = models.PositiveIntegerField()
|
|
|
|
|
2008-08-01 23:54:53 +08:00
|
|
|
class Comparison(models.Model):
|
|
|
|
"""
|
|
|
|
A model that tests having multiple GenericForeignKeys
|
|
|
|
"""
|
|
|
|
comparative = models.CharField(max_length=50)
|
2008-08-12 22:15:38 +08:00
|
|
|
|
2008-08-01 23:54:53 +08: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")
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s is %s than %s" % (self.first_obj, self.comparative, self.other_obj)
|
|
|
|
|
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
|
|
|
|
2007-05-08 18:59:35 +08:00
|
|
|
tags = generic.GenericRelation(TaggedItem)
|
2008-08-12 22:15:38 +08:00
|
|
|
comparisons = generic.GenericRelation(Comparison,
|
2008-08-01 23:54:53 +08:00
|
|
|
object_id_field="object_id1",
|
|
|
|
content_type_field="content_type1")
|
2006-06-17 03:18:30 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.common_name
|
2007-12-09 15:12:07 +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
|
|
|
|
2007-05-08 18:59:35 +08:00
|
|
|
tags = generic.GenericRelation(TaggedItem)
|
2007-12-09 15:12:07 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.name
|
2007-12-09 15:12:07 +08:00
|
|
|
|
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
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-06-17 03:18:30 +08:00
|
|
|
return self.name
|