2007-02-28 23:24:05 +08:00
|
|
|
"""
|
2014-09-24 13:13:13 +08:00
|
|
|
Tests for select_related()
|
2007-02-28 23:24:05 +08:00
|
|
|
|
|
|
|
``select_related()`` follows all relationships and pre-caches any foreign key
|
|
|
|
values so that complex trees can be fetched in a single query. However, this
|
|
|
|
isn't always a good idea, so the ``depth`` argument control how many "levels"
|
|
|
|
the select-related behavior will traverse.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
|
2014-12-05 04:47:48 +08:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
2007-02-28 23:24:05 +08:00
|
|
|
# Who remembers high school biology?
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Domain(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Kingdom(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
domain = models.ForeignKey(Domain)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Phylum(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
kingdom = models.ForeignKey(Kingdom)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Klass(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
phylum = models.ForeignKey(Phylum)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Order(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
klass = models.ForeignKey(Klass)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Family(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
order = models.ForeignKey(Order)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Genus(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
family = models.ForeignKey(Family)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2007-02-28 23:24:05 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2007-02-28 23:24:05 +08:00
|
|
|
class Species(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=50)
|
2007-02-28 23:24:05 +08:00
|
|
|
genus = models.ForeignKey(Genus)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2013-10-15 20:15:02 +08:00
|
|
|
|
|
|
|
# and we'll invent a new thing so we have a model with two foreign keys
|
2013-11-03 05:34:05 +08:00
|
|
|
|
|
|
|
|
2013-10-15 20:15:02 +08:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class HybridSpecies(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
parent_1 = models.ForeignKey(Species, related_name='child_1')
|
|
|
|
parent_2 = models.ForeignKey(Species, related_name='child_2')
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-10-15 20:15:02 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2014-12-05 04:47:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Topping(models.Model):
|
|
|
|
name = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Pizza(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
toppings = models.ManyToManyField(Topping)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class TaggedItem(models.Model):
|
|
|
|
tag = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
content_type = models.ForeignKey(ContentType, related_name='select_related_tagged_items')
|
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
content_object = GenericForeignKey('content_type', 'object_id')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.tag
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Bookmark(models.Model):
|
|
|
|
url = models.URLField()
|
|
|
|
tags = GenericRelation(TaggedItem)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.url
|