75
I would have expected .latest() to always return the most recent instance based on a (date)(time) field.
The documentation says that
If your modelβs
Meta
specifiesget_latest_by
, you can leave off thefield_name
argument tolatest()
. Django will use the field specified inget_latest_by
by default.
All this means is that when you fire MyModel.objects.latest()
you will get the latest instance based on the date/time field. And when I tested your code using sample data, it indeed did.
And then later I wanted instances.latest(), but it would not give me the correct instance, in fact it gave me the first instance.
You have misunderstood the way latest()
works. When called on MyModel.objects it returns
the latest instance in the table. When called on a queryset, latest
will return the first object in the queryset. Your queryset consisted of all instances of MyModel
ordered by creation_date
in ascending order. It is only natural then that latest
on this queryset should return the first row of the queryset. This incidentally happens to be the oldest row in the table.
One way to get a better understanding is to view the query fired for latest
.
Case 1:
from django.db import connection
MyModel.objects.latest()
print connection.queries[-1]['sql']
This prints:
SELECT "app_mymodel"."id", "app_mymodel"."creation_date" FROM
"app_mymodel" ORDER BY "app_mymodel"."creation_date" DESC LIMIT 1
Note the ordering by creation_date DESC
and the LIMIT
clause. The former is thanks to get_latest_by
whereas the latter is the contribution of latest
.
Now, case 2:
MyModel.objects.order_by('creation_date').latest()
print connection.queries[-1]['sql']
prints
SELECT "app_mymodel"."id", "app_mymodel"."creation_date" FROM
"app_mymodel" ORDER BY "app_mymodel"."creation_date" ASC LIMIT 1
Note that the ordering has changed to creation_date ASC
. This is the result of the explicit order_by
. The LIMIT
is tacked on er, later courtesy latest
.
Let us also see Case 3: where you explicitly specify the field_name
for objects.latest()
.
MyModel.objects.latest('id')
print connection.queries[-1]['sql']
shows
SELECT "app_mymodel"."id", "app_mymodel"."creation_date" FROM "app_mymodel"
ORDER BY "app_mymodel"."id" DESC LIMIT 1
9
I guess this is a known bug in Django that was fixed after 1.3 was released.
- [Django]-Simple approach to launching background task in Django
- [Django]-How do I set up Jupyter/IPython Notebook for Django?
- [Django]-How do I match the question mark character in a Django URL?
0
This worked for me
latestsetuplist = SetupTemplate.objects.order_by('-creationTime')[:10][::1]
- [Django]-How to make email field unique in model User from contrib.auth in Django
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."
- [Django]-What's the best way to start learning django?
0
if we have the value of id or date
post_id = BlogPost.objects.get(id=id)
try:
previous_post = BlogPost.objects.all().order_by('id')[post_id.id-2:post_id.id-1]
except:
previous_post = None
try:
next_post = BlogPost.objects.all().order_by('id')[post_id.id:post_id.id+1]
except:
next_post = None
it worked for me, even if an id is missing it picks next or previous value to that
- [Django]-Not able to create super user with Django manage.py
- [Django]-What does "'tests' module incorrectly imported" mean?
- [Django]-Atomic increment of a counter in django