Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14471 -- Corrected a regression in the use of methods on custo…

…m managers on related querysets. Thanks to Lucky for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14389 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8a724802c5b3b8d4f8d51bcf276838bf0f4ba176 1 parent ded416b
Russell Keith-Magee authored October 28, 2010
8  django/db/models/fields/related.py
@@ -420,7 +420,7 @@ def add(self, *objs):
@@ -428,7 +428,7 @@ def get_or_create(self, **kwargs):
@@ -517,7 +517,7 @@ def create(self, **kwargs):
@@ -525,7 +525,7 @@ def create(self, **kwargs):
15  tests/regressiontests/multiple_database/models.py
@@ -30,7 +30,21 @@ def __unicode__(self):
30 30
     class Meta:
31 31
         ordering = ('name',)
32 32
 
  33
+# This book manager doesn't do anything interesting; it just
  34
+# exists to strip out the 'extra_arg' argument to certain
  35
+# calls. This argument is used to establish that the BookManager
  36
+# is actually getting used when it should be.
  37
+class BookManager(models.Manager):
  38
+    def create(self, *args, **kwargs):
  39
+        kwargs.pop('extra_arg', None)
  40
+        return super(BookManager, self).create(*args, **kwargs)
  41
+
  42
+    def get_or_create(self, *args, **kwargs):
  43
+        kwargs.pop('extra_arg', None)
  44
+        return super(BookManager, self).get_or_create(*args, **kwargs)
  45
+
33 46
 class Book(models.Model):
  47
+    objects = BookManager()
34 48
     title = models.CharField(max_length=100)
35 49
     published = models.DateField()
36 50
     authors = models.ManyToManyField(Person)
@@ -60,4 +74,3 @@ class UserProfile(models.Model):
60 74
 
61 75
     class Meta:
62 76
         ordering = ('flavor',)
63  
-
22  tests/regressiontests/multiple_database/tests.py
@@ -891,6 +891,28 @@ def test_subquery(self):
891 891
         except ValueError:
892 892
             pass
893 893
 
  894
+    def test_related_manager(self):
  895
+        "Related managers return managers, not querysets"
  896
+        mark = Person.objects.using('other').create(name="Mark Pilgrim")
  897
+
  898
+        # extra_arg is removed by the BookManager's implementation of
  899
+        # create(); but the BookManager's implementation won't get called
  900
+        # unless edited returns a Manager, not a queryset
  901
+        mark.book_set.create(title="Dive into Python",
  902
+                             published=datetime.date(2009, 5, 4),
  903
+                             extra_arg=True)
  904
+
  905
+        mark.book_set.get_or_create(title="Dive into Python",
  906
+                                    published=datetime.date(2009, 5, 4),
  907
+                                    extra_arg=True)
  908
+
  909
+        mark.edited.create(title="Dive into Water",
  910
+                           published=datetime.date(2009, 5, 4),
  911
+                           extra_arg=True)
  912
+
  913
+        mark.edited.get_or_create(title="Dive into Water",
  914
+                                  published=datetime.date(2009, 5, 4),
  915
+                                  extra_arg=True)
894 916
 
895 917
 class TestRouter(object):
896 918
     # A test router. The behaviour is vaguely master/slave, but the

0 notes on commit 8a72480

Please sign in to comment.
Something went wrong with that request. Please try again.