Permalink
Browse files

Added support for "extra" in specialized query sets by copying the ex…

…tra select.


Fixes #6
  • Loading branch information...
1 parent 94a2568 commit eaecc5400f978a2e1bb7d9d09d9a82b4efd95f0a @gnarea gnarea committed Aug 25, 2011
Showing with 31 additions and 8 deletions.
  1. +6 −0 djeneralize/query.py
  2. +13 −3 docs/source/querying.rst
  3. +1 −2 docs/source/todo.rst
  4. +11 −3 tests/test_base.py
View
@@ -86,6 +86,12 @@ def iterator(self):
# Copy any deferred loading over to the new querysets:
sub_queryset.query.deferred_loading = self.query.deferred_loading
+ # Copy any extra select statements to the new querysets. NB: It
+ # doesn't make sense to copy any of the "where", "tables" or
+ # "order_by" options as these have already been applied in the
+ # parent queryset
+ sub_queryset.query.extra = self.query.extra
+
sub_instances = sub_queryset.in_bulk(ids)
specialized_model_instances.update(sub_instances)
View
@@ -58,7 +58,17 @@ model instances::
.. note:: The :meth:`filter` can only take keyword arguments from the general
model and not from the specialized models as they are meaningless in the
general context.
-
+
+extra()
+-------
+
+All the functionality in this method is honoured, but only the ``select``
+argument is copied over to the cloned
+:class:`~djeneralize.query.SpecializedQuerySet`::
+
+ >>> WritingImplement.specializations.extra(select={'description': 'SELECT "The " || name || " is " || length'}).values_list('description', flat=True)
+ [u'The Crayola is 5', u'The Bic is 3']
+
get()
-----
@@ -93,8 +103,8 @@ return the updated queryset::
>>> final
[<FountainPen: Fountain pen>, <Pen: General pen>, <BallPointPen: Ballpoint pen>, <Pencil: Pencil>]
-annotate(), raw() and extra()
------------------------------
+annotate() and raw()
+--------------------
Unfortunately, due to the complexities of how the above work is performed on the
underlying SQL query instance, it is not trivial to copy these annotations over
View
@@ -17,8 +17,7 @@ over to the ``in_bulk`` call to generate the specializations. The way Django
handles annotations is not trivial and quite a bit of time and effort will be
needed to be invested to resolve this issue.
-Additionally any ``extra()`` and ``raw()`` calls should be supported where
-possible.
+Additionally any ``raw()`` calls should be supported where possible.
Deferred loading of fields
==========================
View
@@ -25,7 +25,6 @@
eq_, ok_, assert_false, raises, assert_raises, assert_not_equal
)
-from djeneralize.models import BaseGeneralizationModel
from djeneralize.utils import *
from .fixtures import *
from .test_djeneralize.writing.models import *
@@ -417,8 +416,17 @@ def test_annotate(self):
assert_raises(
NotImplementedError, WritingImplement.specializations.annotate
- )
-
+ )
+
+ def test_extra(self):
+ """Queries added with .extra() are inherited in specializations."""
+ writing_implement = WritingImplement.specializations.extra(
+ select={'extra_field': 'SELECT 1'},
+ )[0]
+
+ ok_(hasattr(writing_implement, 'extra_field'))
+ eq_(writing_implement.extra_field, 1)
+
def test_ordering(self):
"""Ordering of the initial queryset is respected in the child objects"""

0 comments on commit eaecc54

Please sign in to comment.