| 1 |
""" |
| 2 |
By specifying the 'proxy' Meta attribute, model subclasses can specify that |
| 3 |
they will take data directly from the table of their base class table rather |
| 4 |
than using a new table of their own. This allows them to act as simple proxies, |
| 5 |
providing a modified interface to the data from the base class. |
| 6 |
""" |
| 7 |
|
| 8 |
from django.db import models |
| 9 |
|
| 10 |
# A couple of managers for testing managing overriding in proxy model cases. |
| 11 |
|
| 12 |
|
| 13 |
class PersonManager(models.Manager): |
| 14 |
def get_queryset(self): |
| 15 |
return super().get_queryset().exclude(name="fred") |
| 16 |
|
| 17 |
|
| 18 |
class SubManager(models.Manager): |
| 19 |
def get_queryset(self): |
| 20 |
return super().get_queryset().exclude(name="wilma") |
| 21 |
|
| 22 |
|
| 23 |
class Person(models.Model): |
| 24 |
""" |
| 25 |
A simple concrete base class. |
| 26 |
""" |
| 27 |
|
| 28 |
name = models.CharField(max_length=50) |
| 29 |
|
| 30 |
objects = PersonManager() |
| 31 |
|
| 32 |
def __str__(self): |
| 33 |
return self.name |
| 34 |
|
| 35 |
|
| 36 |
class Abstract(models.Model): |
| 37 |
""" |
| 38 |
A simple abstract base class, to be used for error checking. |
| 39 |
""" |
| 40 |
|
| 41 |
data = models.CharField(max_length=10) |
| 42 |
|
| 43 |
class Meta: |
| 44 |
abstract = True |
| 45 |
|
| 46 |
|
| 47 |
class MyPerson(Person): |
| 48 |
""" |
| 49 |
A proxy subclass, this should not get a new table. Overrides the default |
| 50 |
manager. |
| 51 |
""" |
| 52 |
|
| 53 |
class Meta: |
| 54 |
proxy = True |
| 55 |
ordering = ["name"] |
| 56 |
permissions = (("display_users", "May display users information"),) |
| 57 |
|
| 58 |
objects = SubManager() |
| 59 |
other = PersonManager() |
| 60 |
|
| 61 |
def has_special_name(self): |
| 62 |
return self.name.lower() == "special" |
| 63 |
|
| 64 |
|
| 65 |
class ManagerMixin(models.Model): |
| 66 |
excluder = SubManager() |
| 67 |
|
| 68 |
class Meta: |
| 69 |
abstract = True |
| 70 |
|
| 71 |
|
| 72 |
class OtherPerson(Person, ManagerMixin): |
| 73 |
""" |
| 74 |
A class with the default manager from Person, plus a secondary manager. |
| 75 |
""" |
| 76 |
|
| 77 |
class Meta: |
| 78 |
proxy = True |
| 79 |
ordering = ["name"] |
| 80 |
|
| 81 |
|
| 82 |
class StatusPerson(MyPerson): |
| 83 |
""" |
| 84 |
A non-proxy subclass of a proxy, it should get a new table. |
| 85 |
""" |
| 86 |
|
| 87 |
status = models.CharField(max_length=80) |
| 88 |
|
| 89 |
objects = models.Manager() |
| 90 |
|
| 91 |
|
| 92 |
# We can even have proxies of proxies (and subclass of those). |
| 93 |
|
| 94 |
|
| 95 |
class MyPersonProxy(MyPerson): |
| 96 |
class Meta: |
| 97 |
proxy = True |
| 98 |
|
| 99 |
|
| 100 |
class LowerStatusPerson(MyPersonProxy): |
| 101 |
status = models.CharField(max_length=80) |
| 102 |
|
| 103 |
objects = models.Manager() |
| 104 |
|
| 105 |
|
| 106 |
class User(models.Model): |
| 107 |
name = models.CharField(max_length=100) |
| 108 |
|
| 109 |
def __str__(self): |
| 110 |
return self.name |
| 111 |
|
| 112 |
|
| 113 |
class UserProxy(User): |
| 114 |
class Meta: |
| 115 |
proxy = True |
| 116 |
|
| 117 |
|
| 118 |
class AnotherUserProxy(User): |
| 119 |
class Meta: |
| 120 |
proxy = True |
| 121 |
|
| 122 |
|
| 123 |
class UserProxyProxy(UserProxy): |
| 124 |
class Meta: |
| 125 |
proxy = True |
| 126 |
|
| 127 |
|
| 128 |
class MultiUserProxy(UserProxy, AnotherUserProxy): |
| 129 |
class Meta: |
| 130 |
proxy = True |
| 131 |
|
| 132 |
|
| 133 |
# We can still use `select_related()` to include related models in our querysets. |
| 134 |
|
| 135 |
|
| 136 |
class Country(models.Model): |
| 137 |
name = models.CharField(max_length=50) |
| 138 |
|
| 139 |
|
| 140 |
class State(models.Model): |
| 141 |
name = models.CharField(max_length=50) |
| 142 |
country = models.ForeignKey(Country, models.CASCADE) |
| 143 |
|
| 144 |
def __str__(self): |
| 145 |
return self.name |
| 146 |
|
| 147 |
|
| 148 |
class StateProxy(State): |
| 149 |
class Meta: |
| 150 |
proxy = True |
| 151 |
|
| 152 |
|
| 153 |
# Proxy models still works with filters (on related fields) |
| 154 |
# and select_related, even when mixed with model inheritance |
| 155 |
|
| 156 |
|
| 157 |
class BaseUser(models.Model): |
| 158 |
name = models.CharField(max_length=255) |
| 159 |
|
| 160 |
def __str__(self): |
| 161 |
return ":".join( |
| 162 |
( |
| 163 |
self.__class__.__name__, |
| 164 |
self.name, |
| 165 |
) |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
class TrackerUser(BaseUser): |
| 170 |
status = models.CharField(max_length=50) |
| 171 |
|
| 172 |
|
| 173 |
class ProxyTrackerUser(TrackerUser): |
| 174 |
class Meta: |
| 175 |
proxy = True |
| 176 |
|
| 177 |
|
| 178 |
class Issue(models.Model): |
| 179 |
summary = models.CharField(max_length=255) |
| 180 |
assignee = models.ForeignKey( |
| 181 |
ProxyTrackerUser, models.CASCADE, related_name="issues" |
| 182 |
) |
| 183 |
|
| 184 |
def __str__(self): |
| 185 |
return ":".join( |
| 186 |
( |
| 187 |
self.__class__.__name__, |
| 188 |
self.summary, |
| 189 |
) |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
class Bug(Issue): |
| 194 |
version = models.CharField(max_length=50) |
| 195 |
reporter = models.ForeignKey(BaseUser, models.CASCADE) |
| 196 |
|
| 197 |
|
| 198 |
class ProxyBug(Bug): |
| 199 |
""" |
| 200 |
Proxy of an inherited class |
| 201 |
""" |
| 202 |
|
| 203 |
class Meta: |
| 204 |
proxy = True |
| 205 |
|
| 206 |
|
| 207 |
class ProxyProxyBug(ProxyBug): |
| 208 |
""" |
| 209 |
A proxy of proxy model with related field |
| 210 |
""" |
| 211 |
|
| 212 |
class Meta: |
| 213 |
proxy = True |
| 214 |
|
| 215 |
|
| 216 |
class Improvement(Issue): |
| 217 |
""" |
| 218 |
A model that has relation to a proxy model |
| 219 |
or to a proxy of proxy model |
| 220 |
""" |
| 221 |
|
| 222 |
version = models.CharField(max_length=50) |
| 223 |
reporter = models.ForeignKey(ProxyTrackerUser, models.CASCADE) |
| 224 |
associated_bug = models.ForeignKey(ProxyProxyBug, models.CASCADE) |
| 225 |
|
| 226 |
|
| 227 |
class ProxyImprovement(Improvement): |
| 228 |
class Meta: |
| 229 |
proxy = True |