Re: basci question about queryset for retrieving top element
I am a Django newbie and also interested in this question.
From reading the Django book http://www.djangobook.com/en/2.0/chapter05/,
I see that the query 'Publisher.objects.order_by('name')[0:2]' maps
to:
SELECT id, name, address, city, state_province, country, website
FROM books_publisher
ORDER BY name
OFFSET 0 LIMIT 2;
Also, Django delays execution of the db query until the result is
actually required.
Given the above two observations, I would think that both the type of
accesses you mentioned will do exactly the same thing, which is to do
a query with "OFFSET 0 LIMIT 1".
To confirm this, you can use the django-devserver (https://github.com/
dcramer/django-devserver) with SQL queries enabled which will display
every query made in real time.
thanks,
-pavan
On Apr 14, 9:56 pm, NENAD CIKIC <nenad.ci...@gmail.com> wrote:
> Hello!
> I want to get just the top element of one queryset. SO I have
> something as
> qs=MyModel.objects.filter(...).order_by(..)
>
> and then use qs[0] and check some value
>
> I wonder now if i did the right thing or it is better to use .extra on
> queryset.
> Is the queryset object allocated for all objects, or is django/python
> smarter than me and gets only the needed object?
>
> Should I maybe do directly
> topObj=MyModel.objects.filter(...).order_by(..)[0]
>
> What is better?
> Thanks
> Nenad
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home