2010/11/10 Łukasz Rekucki <lreku...@gmail.com>:
> On 10 November 2010 21:55, Shawn Milochik <sh...@milochik.com> wrote:
>> The queryset returns zero or more instances of your model. So if
>> there's only one result, you can just get the first item in the
>> queryset by its index.
>>
>> For example:
>>
>> some_queryset = YourModel.objects.filter(**kwargs)
>>
>> if some_queryset.count() == 1:
>>    x = some_queryset[0]
>>    #x is now an instance of your model.
>> else:
>>    raise ValueError("%d results -- expected one!" % (some_queryset.count(),)
>
> This is a very bad way of checking queryset length, because count()
> actually creates a new QuerySet that gets executed (again!). Instead
> use len() that will first check against queryset's internal cache.

I agree that your way is better, but I disagree about duplicate
execution -- doing a count() will simply run a SQL query that does a
COUNT. It's not doing anything "again" because the original queryset
hasn't actually been executed (lazy) until its contents are actually
used (as in the variable assignment), and the COUNT statement is
lightweight.

Shawn

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to