PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/tests/modeltests/proxy_models/models.py

https://code.google.com/p/mango-py/
Python | 164 lines | 163 code | 0 blank | 1 comment | 0 complexity | 2e02d2bdf3ca7ccea691209cf37640f4 MD5 | raw file
  1"""
  2By specifying the 'proxy' Meta attribute, model subclasses can specify that
  3they will take data directly from the table of their base class table rather
  4than using a new table of their own. This allows them to act as simple proxies,
  5providing a modified interface to the data from the base class.
  6"""
  7
  8from django.contrib.contenttypes.models import ContentType
  9from django.db import models
 10
 11
 12# A couple of managers for testing managing overriding in proxy model cases.
 13
 14class PersonManager(models.Manager):
 15    def get_query_set(self):
 16        return super(PersonManager, self).get_query_set().exclude(name="fred")
 17
 18class SubManager(models.Manager):
 19    def get_query_set(self):
 20        return super(SubManager, self).get_query_set().exclude(name="wilma")
 21
 22class Person(models.Model):
 23    """
 24    A simple concrete base class.
 25    """
 26    name = models.CharField(max_length=50)
 27
 28    objects = PersonManager()
 29
 30    def __unicode__(self):
 31        return self.name
 32
 33class Abstract(models.Model):
 34    """
 35    A simple abstract base class, to be used for error checking.
 36    """
 37    data = models.CharField(max_length=10)
 38
 39    class Meta:
 40        abstract = True
 41
 42class MyPerson(Person):
 43    """
 44    A proxy subclass, this should not get a new table. Overrides the default
 45    manager.
 46    """
 47    class Meta:
 48        proxy = True
 49        ordering = ["name"]
 50
 51    objects = SubManager()
 52    other = PersonManager()
 53
 54    def has_special_name(self):
 55        return self.name.lower() == "special"
 56
 57class ManagerMixin(models.Model):
 58    excluder = SubManager()
 59
 60    class Meta:
 61        abstract = True
 62
 63class OtherPerson(Person, ManagerMixin):
 64    """
 65    A class with the default manager from Person, plus an secondary manager.
 66    """
 67    class Meta:
 68        proxy = True
 69        ordering = ["name"]
 70
 71class StatusPerson(MyPerson):
 72    """
 73    A non-proxy subclass of a proxy, it should get a new table.
 74    """
 75    status = models.CharField(max_length=80)
 76
 77# We can even have proxies of proxies (and subclass of those).
 78class MyPersonProxy(MyPerson):
 79    class Meta:
 80        proxy = True
 81
 82class LowerStatusPerson(MyPersonProxy):
 83    status = models.CharField(max_length=80)
 84
 85class User(models.Model):
 86    name = models.CharField(max_length=100)
 87
 88    def __unicode__(self):
 89        return self.name
 90
 91class UserProxy(User):
 92    class Meta:
 93        proxy = True
 94
 95class UserProxyProxy(UserProxy):
 96    class Meta:
 97        proxy = True
 98
 99# We can still use `select_related()` to include related models in our querysets.
100class Country(models.Model):
101    name = models.CharField(max_length=50)
102
103class State(models.Model):
104    name = models.CharField(max_length=50)
105    country = models.ForeignKey(Country)
106
107    def __unicode__(self):
108        return self.name
109
110class StateProxy(State):
111    class Meta:
112        proxy = True
113
114# Proxy models still works with filters (on related fields)
115# and select_related, even when mixed with model inheritance
116class BaseUser(models.Model):
117    name = models.CharField(max_length=255)
118
119class TrackerUser(BaseUser):
120    status = models.CharField(max_length=50)
121
122class ProxyTrackerUser(TrackerUser):
123    class Meta:
124        proxy = True
125
126
127class Issue(models.Model):
128    summary = models.CharField(max_length=255)
129    assignee = models.ForeignKey(TrackerUser)
130
131    def __unicode__(self):
132        return ':'.join((self.__class__.__name__,self.summary,))
133
134class Bug(Issue):
135    version = models.CharField(max_length=50)
136    reporter = models.ForeignKey(BaseUser)
137
138class ProxyBug(Bug):
139    """
140    Proxy of an inherited class
141    """
142    class Meta:
143        proxy = True
144
145
146class ProxyProxyBug(ProxyBug):
147    """
148    A proxy of proxy model with related field
149    """
150    class Meta:
151        proxy = True
152
153class Improvement(Issue):
154    """
155    A model that has relation to a proxy model
156    or to a proxy of proxy model
157    """
158    version = models.CharField(max_length=50)
159    reporter = models.ForeignKey(ProxyTrackerUser)
160    associated_bug = models.ForeignKey(ProxyProxyBug)
161
162class ProxyImprovement(Improvement):
163    class Meta:
164        proxy = True