| 1 |
""" |
| 2 |
Tests for select_related() |
| 3 |
|
| 4 |
``select_related()`` follows all relationships and pre-caches any foreign key |
| 5 |
values so that complex trees can be fetched in a single query. However, this |
| 6 |
isn't always a good idea, so the ``depth`` argument control how many "levels" |
| 7 |
the select-related behavior will traverse. |
| 8 |
""" |
| 9 |
|
| 10 |
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation |
| 11 |
from django.contrib.contenttypes.models import ContentType |
| 12 |
from django.db import models |
| 13 |
|
| 14 |
# Who remembers high school biology? |
| 15 |
|
| 16 |
|
| 17 |
class Domain(models.Model): |
| 18 |
name = models.CharField(max_length=50) |
| 19 |
|
| 20 |
|
| 21 |
class Kingdom(models.Model): |
| 22 |
name = models.CharField(max_length=50) |
| 23 |
domain = models.ForeignKey(Domain, models.CASCADE) |
| 24 |
|
| 25 |
|
| 26 |
class Phylum(models.Model): |
| 27 |
name = models.CharField(max_length=50) |
| 28 |
kingdom = models.ForeignKey(Kingdom, models.CASCADE) |
| 29 |
|
| 30 |
|
| 31 |
class Klass(models.Model): |
| 32 |
name = models.CharField(max_length=50) |
| 33 |
phylum = models.ForeignKey(Phylum, models.CASCADE) |
| 34 |
|
| 35 |
|
| 36 |
class Order(models.Model): |
| 37 |
name = models.CharField(max_length=50) |
| 38 |
klass = models.ForeignKey(Klass, models.CASCADE) |
| 39 |
|
| 40 |
|
| 41 |
class Family(models.Model): |
| 42 |
name = models.CharField(max_length=50) |
| 43 |
order = models.ForeignKey(Order, models.CASCADE) |
| 44 |
|
| 45 |
|
| 46 |
class Genus(models.Model): |
| 47 |
name = models.CharField(max_length=50) |
| 48 |
family = models.ForeignKey(Family, models.CASCADE) |
| 49 |
|
| 50 |
|
| 51 |
class Species(models.Model): |
| 52 |
name = models.CharField(max_length=50) |
| 53 |
genus = models.ForeignKey(Genus, models.CASCADE) |
| 54 |
|
| 55 |
|
| 56 |
# and we'll invent a new thing so we have a model with two foreign keys |
| 57 |
|
| 58 |
|
| 59 |
class HybridSpecies(models.Model): |
| 60 |
name = models.CharField(max_length=50) |
| 61 |
parent_1 = models.ForeignKey(Species, models.CASCADE, related_name="child_1") |
| 62 |
parent_2 = models.ForeignKey(Species, models.CASCADE, related_name="child_2") |
| 63 |
|
| 64 |
|
| 65 |
class Topping(models.Model): |
| 66 |
name = models.CharField(max_length=30) |
| 67 |
|
| 68 |
|
| 69 |
class Pizza(models.Model): |
| 70 |
name = models.CharField(max_length=100) |
| 71 |
toppings = models.ManyToManyField(Topping) |
| 72 |
|
| 73 |
|
| 74 |
class TaggedItem(models.Model): |
| 75 |
tag = models.CharField(max_length=30) |
| 76 |
|
| 77 |
content_type = models.ForeignKey( |
| 78 |
ContentType, models.CASCADE, related_name="select_related_tagged_items" |
| 79 |
) |
| 80 |
object_id = models.PositiveIntegerField() |
| 81 |
content_object = GenericForeignKey("content_type", "object_id") |
| 82 |
|
| 83 |
|
| 84 |
class Bookmark(models.Model): |
| 85 |
url = models.URLField() |
| 86 |
tags = GenericRelation(TaggedItem) |