mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-04-15 00:51:02 +00:00
Merge pull request #5 from smacker/master
get_subclass method - thanks smacker!
This commit is contained in:
commit
4e361a20f8
4 changed files with 14 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ HGREV
|
|||
.coverage
|
||||
.tox/
|
||||
Django-*.egg
|
||||
*.pyc
|
||||
|
|
@ -263,6 +263,11 @@ be returned as their actual type, you can pass subclass names to
|
|||
nearby_places = Place.objects.select_subclasses("restaurant")
|
||||
# restaurants will be Restaurant instances, bars will still be Place instances
|
||||
|
||||
Also it provides syntax sugar for `get()` method::
|
||||
|
||||
place = Place.objects.get_subclass(id=some_id)
|
||||
# "place" will automatically be an instance of Place, Restaurant, or Bar
|
||||
|
||||
If you don't explicitly call ``select_subclasses()``, an ``InheritanceManager``
|
||||
behaves identically to a normal ``Manager``; so it's safe to use as your
|
||||
default manager for the model.
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@ class InheritanceManager(models.Manager):
|
|||
def select_subclasses(self, *subclasses):
|
||||
return self.get_query_set().select_subclasses(*subclasses)
|
||||
|
||||
def get_subclass(self, *args, **kwargs):
|
||||
return self.get_query_set().select_subclasses().get(*args, **kwargs)
|
||||
|
||||
|
||||
class InheritanceCastMixin(object):
|
||||
def cast(self):
|
||||
|
|
|
|||
|
|
@ -353,6 +353,11 @@ if django.VERSION >= (1, 2):
|
|||
set([self.child1,
|
||||
InheritanceManagerTestParent(pk=self.child2.pk)]))
|
||||
|
||||
def test_get_subclass(self):
|
||||
self.assertEquals(
|
||||
self.get_manager().get_subclass(pk=self.child1.pk),
|
||||
self.child1)
|
||||
|
||||
|
||||
class InheritanceManagerRelatedTests(InheritanceManagerTests):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue