Order_by on queryset from chained filters not working

72 views
Skip to first unread message

Shawn H

unread,
Apr 24, 2014, 3:07:07 PM4/24/14
to django...@googlegroups.com
I've a search page that allows users to search for a "ZoningCase" by many different parameters, some from the ZoningCase object and some from several related objects.  The user can combine 1 or more of these parameters for their search.  My view chains together filters on the queryset based on the parameters selected.  All of this works fine. After I've finished chaining filters, I add one additional order_by chain, to sort the cases by case_number.  This works, in that the select query django builds is correct, having the correct order by clause at the end.

Next, to prepare the cases for display, I iterate through the queryset and create a custom class for each case, appending that class to a list.  Here, it seems, the ordering breaks down.  It's as if the iterator ignores the order_by clause and just loops through however it feels like! As I begin the loop, it calls into query.py, the __iter__ method, and looking at self._result_cache, my ZoningCase objects in the queryset are unordered (e.g., 109, 129, 101,...).  I can easily fix it by sorting my final list, but since order_by has always worked for me when doing simply queries, I'm wondering if I'm doing something wrong.  My relevant code is below.  Thanks in advance

for key, value in form.cleaned_data.items():
if key == 'council_district':
if value.count() > 0:
if cases == None:
cases = ZoningCase.objects.filter(councildistrict__in=value)
else:
cases = cases.filter(councildistrict__in=value)
if key == 'cpc':
case_dates = CaseCPCDate.objects.filter(cpc_meeting=value)
if case_dates.count() > 0:
if cases == None:
cases = ZoningCase.objects.filter(casecpcdate__in=case_dates)
else:
cases = cases.filter(casecpcdate__in=case_dates)
if key == 'cc':
case_dates = CaseCCDate.objects.filter(cc_meeting=value)
if case_dates.count() > 0:
if cases == None:
cases = ZoningCase.objects.filter(caseccdate__in=case_dates)
else:
cases = cases.filter(caseccdate__in=case_dates)
if key == 'request_type':
if value.count() > 0:
if cases == None:
cases = ZoningCase.objects.filter(zoningrequest__request_type=value)
else:
cases = cases.filter(zoningrequest__request_type=value)
if value is not None and value != '' and value != False and key != 'cpc' and key != 'cc' and key != 'council_district' and key != 'request_type':
if cases == None:
cases = ZoningCase.objects.filter(**{key: value})
else:
cases = cases.filter(**{key: value})
if cases != None:
cases.order_by('case_number')
for c in cases:
zoningResults.append(ZoningSearchResult(c))

Joseph Mutumi

unread,
Apr 24, 2014, 3:52:20 PM4/24/14
to django...@googlegroups.com
Just wondering why not cases = cases.order_by('case_number') ?
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f6ed6295-4a11-44e2-981d-15be8d1f79c0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Shawn H

unread,
Apr 24, 2014, 3:56:44 PM4/24/14
to django...@googlegroups.com
Good catch.  Unfortunately, that didn't resolve the issue.  Still not sorting as expected.  Thanks.

Shawn H

unread,
Apr 24, 2014, 3:59:03 PM4/24/14
to django...@googlegroups.com
Or rather, I forgot to restart my apache instance.  That DID resolve the issue.  What a simply little thing, no?  Appreciate the help.
Reply all
Reply to author
Forward
0 new messages