Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14471 -- Corrected a regression in the use of methods …

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

Backport of r14389 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14390 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 29ea1bef215040d34fb0f03c7f39f3a82515febf 1 parent 3a679cc
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
@@ -890,6 +890,28 @@ def test_subquery(self):
890 890
         except ValueError:
891 891
             pass
892 892
 
  893
+    def test_related_manager(self):
  894
+        "Related managers return managers, not querysets"
  895
+        mark = Person.objects.using('other').create(name="Mark Pilgrim")
  896
+
  897
+        # extra_arg is removed by the BookManager's implementation of
  898
+        # create(); but the BookManager's implementation won't get called
  899
+        # unless edited returns a Manager, not a queryset
  900
+        mark.book_set.create(title="Dive into Python",
  901
+                             published=datetime.date(2009, 5, 4),
  902
+                             extra_arg=True)
  903
+
  904
+        mark.book_set.get_or_create(title="Dive into Python",
  905
+                                    published=datetime.date(2009, 5, 4),
  906
+                                    extra_arg=True)
  907
+
  908
+        mark.edited.create(title="Dive into Water",
  909
+                           published=datetime.date(2009, 5, 4),
  910
+                           extra_arg=True)
  911
+
  912
+        mark.edited.get_or_create(title="Dive into Water",
  913
+                                  published=datetime.date(2009, 5, 4),
  914
+                                  extra_arg=True)
893 915
 
894 916
 class TestRouter(object):
895 917
     # A test router. The behaviour is vaguely master/slave, but the

0 notes on commit 29ea1be

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