Monday, March 18, 2013

How to get ten random records

No really, I want a maximum of ten random records from the database
(Django 1.4, Postgres 9.1).


Looking at the docs this seems to be what I want ...

Items.objects.filter(name__icontains=term).order_by('?')[:10]

Is there a better way?

Would .order_by() be quicker if I wasn't too concerned about true
randomness?

Do I need try: except: pass to silence errors if there aren't any results?

Thanks

Mike



--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate