how can I order the results of a query filter?


how can I order the results of a query filter?



I have this line of code in my views that allows me to display a group of items by date (I've also reversed the order so the most recent displays first):

currentlinks = Current.objects.order_by('date_added').reverse()[:5] 

works fine, but however, when I concatenate the order_by code with a filter...

currentsources = Current.objects.filter(source__exact='bbc').order_by('date_added') 

why doesn't this doesn't work? poor syntax perhaps, or am I just not understanding how this is supposed to work?




Django: Why are my forms' textfields and imagefields never instantiated but all other fields instantiate ok?

1:



Django re-orden form fields?
If there are no Current objects with a source of "bbc", then you will of course get an empty result set.


how validate form and show the value of the filled fields?
Is that what you mean by doesn't work? If not, please post the results you do get..
Django admin file upload doesn't work, how do I debug this?
Update: One more thing to try: fire up.
How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message
manage.py shell 
and then in the shell, evaluate the queryset which is giving the problem..
Pinax Signup and Accounts
currentsources = Current.objects.filter(source__exact='bbc').order_by('date_added') 
Then, do the following:.
Django validate and go back to preview URL?
from django.db.import connection connection.queries 
and this will show the raw SQL which is executed for the queryset.


Django web application — how to access local harddrive?
It should help home in on the issue..


2:


One thing that I noticed in your query: if you're trying to reverse the order so that more recent dates appear first, you would use the following syntax:. currentlinks = Current.objects.order_by('-date_added'). The minus sign in front on the field reverses the order.

It equates roughly to the following syntax in SQL:. SELECT * FROM current_links ORDER BY date_added DESC. The order_by() should work if you chain it after a filter(), provided that the filter() is valid..



76 out of 100 based on 46 user ratings 346 reviews