2007-02-28 15:24:05 +00:00
|
|
|
"""
|
2014-09-24 05:13:13 +00:00
|
|
|
Tests for select_related()
|
2007-02-28 15:24:05 +00: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.
|
|
|
|
"""
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2007-02-28 15:24:05 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
# Who remembers high school biology?
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Domain(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Kingdom(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
domain = models.ForeignKey(Domain, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Phylum(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
kingdom = models.ForeignKey(Kingdom, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Klass(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
phylum = models.ForeignKey(Phylum, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Order(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
klass = models.ForeignKey(Klass, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Family(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
order = models.ForeignKey(Order, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Genus(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
family = models.ForeignKey(Family, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2007-02-28 15:24:05 +00:00
|
|
|
class Species(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
name = models.CharField(max_length=50)
|
2015-07-22 14:43:21 +00:00
|
|
|
genus = models.ForeignKey(Genus, models.CASCADE)
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2013-10-15 12:15:02 +00:00
|
|
|
# and we'll invent a new thing so we have a model with two foreign keys
|
2013-11-02 21:34:05 +00:00
|
|
|
|
|
|
|
|
2013-10-15 12:15:02 +00:00
|
|
|
class HybridSpecies(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
2022-02-03 19:24:19 +00:00
|
|
|
parent_1 = models.ForeignKey(Species, models.CASCADE, related_name="child_1")
|
|
|
|
parent_2 = models.ForeignKey(Species, models.CASCADE, related_name="child_2")
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2014-12-04 20:47:48 +00:00
|
|
|
|
|
|
|
class Topping(models.Model):
|
|
|
|
name = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
|
|
|
|
class Pizza(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
toppings = models.ManyToManyField(Topping)
|
|
|
|
|
|
|
|
|
|
|
|
class TaggedItem(models.Model):
|
|
|
|
tag = models.CharField(max_length=30)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
content_type = models.ForeignKey(
|
|
|
|
ContentType, models.CASCADE, related_name="select_related_tagged_items"
|
|
|
|
)
|
2014-12-04 20:47:48 +00:00
|
|
|
object_id = models.PositiveIntegerField()
|
2022-02-03 19:24:19 +00:00
|
|
|
content_object = GenericForeignKey("content_type", "object_id")
|
2014-12-04 20:47:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Bookmark(models.Model):
|
|
|
|
url = models.URLField()
|
|
|
|
tags = GenericRelation(TaggedItem)
|