summaryrefslogtreecommitdiff
path: root/tests/select_related/models.py
blob: 04aeaa1b61b41267021ef0bb11e567f2fcbdaa77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Tests for select_related()

``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.contrib.contenttypes.fields import (
    GenericForeignKey, GenericRelation,
)
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.encoding import python_2_unicode_compatible


# Who remembers high school biology?


@python_2_unicode_compatible
class Domain(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Kingdom(models.Model):
    name = models.CharField(max_length=50)
    domain = models.ForeignKey(Domain)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Phylum(models.Model):
    name = models.CharField(max_length=50)
    kingdom = models.ForeignKey(Kingdom)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Klass(models.Model):
    name = models.CharField(max_length=50)
    phylum = models.ForeignKey(Phylum)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Order(models.Model):
    name = models.CharField(max_length=50)
    klass = models.ForeignKey(Klass)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Family(models.Model):
    name = models.CharField(max_length=50)
    order = models.ForeignKey(Order)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Genus(models.Model):
    name = models.CharField(max_length=50)
    family = models.ForeignKey(Family)

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Species(models.Model):
    name = models.CharField(max_length=50)
    genus = models.ForeignKey(Genus)

    def __str__(self):
        return self.name

# and we'll invent a new thing so we have a model with two foreign keys


@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')

    def __str__(self):
        return self.name


@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