mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
@@ -9,14 +9,17 @@ from django.utils.encoding import python_2_unicode_compatible
|
||||
|
||||
# A couple of managers for testing managing overriding in proxy model cases.
|
||||
|
||||
|
||||
class PersonManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super(PersonManager, self).get_queryset().exclude(name="fred")
|
||||
|
||||
|
||||
class SubManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super(SubManager, self).get_queryset().exclude(name="wilma")
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Person(models.Model):
|
||||
"""
|
||||
@@ -29,6 +32,7 @@ class Person(models.Model):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Abstract(models.Model):
|
||||
"""
|
||||
A simple abstract base class, to be used for error checking.
|
||||
@@ -38,6 +42,7 @@ class Abstract(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class MyPerson(Person):
|
||||
"""
|
||||
A proxy subclass, this should not get a new table. Overrides the default
|
||||
@@ -56,12 +61,14 @@ class MyPerson(Person):
|
||||
def has_special_name(self):
|
||||
return self.name.lower() == "special"
|
||||
|
||||
|
||||
class ManagerMixin(models.Model):
|
||||
excluder = SubManager()
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class OtherPerson(Person, ManagerMixin):
|
||||
"""
|
||||
A class with the default manager from Person, plus an secondary manager.
|
||||
@@ -70,6 +77,7 @@ class OtherPerson(Person, ManagerMixin):
|
||||
proxy = True
|
||||
ordering = ["name"]
|
||||
|
||||
|
||||
class StatusPerson(MyPerson):
|
||||
"""
|
||||
A non-proxy subclass of a proxy, it should get a new table.
|
||||
@@ -77,13 +85,17 @@ class StatusPerson(MyPerson):
|
||||
status = models.CharField(max_length=80)
|
||||
|
||||
# We can even have proxies of proxies (and subclass of those).
|
||||
|
||||
|
||||
class MyPersonProxy(MyPerson):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class LowerStatusPerson(MyPersonProxy):
|
||||
status = models.CharField(max_length=80)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class User(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
@@ -91,18 +103,23 @@ class User(models.Model):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class UserProxy(User):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class UserProxyProxy(UserProxy):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
# We can still use `select_related()` to include related models in our querysets.
|
||||
|
||||
|
||||
class Country(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class State(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
@@ -111,12 +128,15 @@ class State(models.Model):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class StateProxy(State):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
# Proxy models still works with filters (on related fields)
|
||||
# and select_related, even when mixed with model inheritance
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class BaseUser(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
@@ -124,9 +144,11 @@ class BaseUser(models.Model):
|
||||
def __str__(self):
|
||||
return ':'.join((self.__class__.__name__, self.name,))
|
||||
|
||||
|
||||
class TrackerUser(BaseUser):
|
||||
status = models.CharField(max_length=50)
|
||||
|
||||
|
||||
class ProxyTrackerUser(TrackerUser):
|
||||
class Meta:
|
||||
proxy = True
|
||||
@@ -140,10 +162,12 @@ class Issue(models.Model):
|
||||
def __str__(self):
|
||||
return ':'.join((self.__class__.__name__, self.summary,))
|
||||
|
||||
|
||||
class Bug(Issue):
|
||||
version = models.CharField(max_length=50)
|
||||
reporter = models.ForeignKey(BaseUser)
|
||||
|
||||
|
||||
class ProxyBug(Bug):
|
||||
"""
|
||||
Proxy of an inherited class
|
||||
@@ -159,6 +183,7 @@ class ProxyProxyBug(ProxyBug):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class Improvement(Issue):
|
||||
"""
|
||||
A model that has relation to a proxy model
|
||||
@@ -168,6 +193,7 @@ class Improvement(Issue):
|
||||
reporter = models.ForeignKey(ProxyTrackerUser)
|
||||
associated_bug = models.ForeignKey(ProxyProxyBug)
|
||||
|
||||
|
||||
class ProxyImprovement(Improvement):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
Reference in New Issue
Block a user