SHARE
TWEET

InheritanceQuerySet annotate patch

a guest Dec 27th, 2011 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # HG changeset patch
  2. # User Facundo Gaich <facugaich@gmail.com>
  3. # Date 1325011816 10800
  4. # Node ID d13f6f7fe1bc3ac4cc108f758dbc8d43b50a13a6
  5. # Parent  8113fd6b7bf64ce35fe254bc5a816d792ae9c654
  6. Change _clone logic to keep annotations done before select_subclasses
  7.  
  8. diff -r 8113fd6b7bf6 -r d13f6f7fe1bc model_utils/managers.py
  9. --- a/model_utils/managers.py   Fri Dec 23 21:03:04 2011 -0500
  10. +++ b/model_utils/managers.py   Tue Dec 27 15:50:16 2011 -0300
  11. @@ -8,10 +8,6 @@
  12.  from django.db.models.query import QuerySet
  13.  
  14.  class InheritanceQuerySet(QuerySet):
  15. -    def __init__(self, *args, **kwargs):
  16. -        self._annotated = None
  17. -        super(InheritanceQuerySet, self).__init__(*args, **kwargs)
  18. -
  19.      def select_subclasses(self, *subclasses):
  20.          if not subclasses:
  21.              subclasses = [o for o in dir(self.model)
  22. @@ -22,11 +18,9 @@
  23.          return new_qs
  24.  
  25.      def _clone(self, klass=None, setup=False, **kwargs):
  26. -        try:
  27. -            kwargs.update({'subclasses': self.subclasses,
  28. -                           '_annotated': self._annotated})
  29. -        except AttributeError:
  30. -            pass
  31. +        for name in ['subclasses', '_annotated']:
  32. +            if hasattr(self, name):
  33. +                kwargs[name] = getattr(self, name)
  34.          return super(InheritanceQuerySet, self)._clone(klass, setup, **kwargs)
  35.  
  36.      def annotate(self, *args, **kwargs):
  37. @@ -40,7 +34,7 @@
  38.              for obj in iter:
  39.                  sub_obj = [getattr(obj, s) for s in self.subclasses if getattr(obj, s)] or [obj]
  40.                  sub_obj = sub_obj[0]
  41. -                if self._annotated:
  42. +                if getattr(self, '_annotated', False):
  43.                      for k in self._annotated:
  44.                          setattr(sub_obj, k, getattr(obj, k))
  45.                      
  46. diff -r 8113fd6b7bf6 -r d13f6f7fe1bc model_utils/tests/tests.py
  47. --- a/model_utils/tests/tests.py        Fri Dec 23 21:03:04 2011 -0500
  48. +++ b/model_utils/tests/tests.py        Tue Dec 27 15:50:16 2011 -0300
  49. @@ -281,6 +281,14 @@
  50.          qs = InheritanceManagerTestParent.objects.select_subclasses().annotate(test_count=models.Count('id'))
  51.          self.assertEqual(qs.get(id=self.child1.id).test_count, 1)
  52.          
  53. +    def test_annotate_before_select_subclasses(self):
  54. +        qs = InheritanceManagerTestParent.objects.annotate(models.Count('id')).select_subclasses()
  55. +        self.assertEqual(qs.get(id=self.child1.id).id__count, 1)
  56. +        
  57. +    def test_annotate_with_named_arguments_before_select_subclasses(self):
  58. +        qs = InheritanceManagerTestParent.objects.annotate(test_count=models.Count('id')).select_subclasses()
  59. +        self.assertEqual(qs.get(id=self.child1.id).test_count, 1)        
  60. +        
  61.          
  62.  class TimeStampedModelTests(TestCase):
  63.      def test_created(self):
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top