I'm making a Django app and I have a view that display both sides of an object_set (a reverse many to many). Because of this, I want to query all of the objects on both sides at the same time. Specifically speaking, I want to have all of the Signup objects that are associated with each Event.
(The view page format should look like this.)
Event (0)
-- Signup (0.0)
-- Signup (0.1)
-- Signup (0.2)
-- Signup (0.3)
Event (1)
-- Signup (1.0)
-- Signup (1.1)
Event (3)
-- Signup (3.0)
-- Signup (3.1)
-- Signup (3.2)
-- Signup (3.3)
...
The code is as follows:
class TournamentDetailView(DetailView):
model = Tournament
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
tournament_id = self.get_object().pk
events = Event.objects.annotate(
cached_signups=(
Signup.objects
.filter(event_id=OuterRef('pk'), tournament_id=tournament_id, dropped=False)
.order_by('created')
.defer('tournament')
)
).all()
context['events'] = events
return context
Here's the traceback:
Traceback:
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\detail.py" in get
106. context = self.get_context_data(object=self.object)
File "C:\Users\werdn\PycharmProjects\gwspo-signups-website\gwhs_speech_and_debate\tournament_signups\views.py" in get_context_data
171. .defer('tournament')
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\werdn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\models\query.py" in annotate
1000. if alias in annotations and annotation.contains_aggregate:
Exception Type: AttributeError at /tournaments/detail/lobo-howl/
Exception Value: 'Query' object has no attribute 'contains_aggregate'
I'm not sure why this is happening and it seems to be happening on the Signups.objects query but even with Signups.objects.all(), this Exception seems to be triggered. That leads me to believe that this is not an issue with the use of OuterRef('pk').
Signupquery in aSubquery()(see documentation here) but that only works for specific values, not for the entire objects. If you just want to optimise your database fetches, you should use prefetch_related