Re: How to get ten random records

2013-03-22 Thread Peter of the Norse
Don’t listen to him. .order_by('?') is the right thing. If you’re not sure, 
install django-debug-toolbar. You’ll see that you get "ORDER BY random()". 

Also, while .get() will throw an exception if the QuerySet is empty, nothing 
else will.

On Mar 18, 2013, at 10:46 PM, Mike Dewhirst wrote:

> On 19/03/2013 3:29pm, Christophe Pettus wrote:
>> 
>> On Mar 18, 2013, at 9:03 PM, Mike Dewhirst wrote:
>> 
>>> No really, I want a maximum of ten random records from the database
>>> (Django 1.4, Postgres 9.1).
>> 
>> This is a case where the raw SQL interface might be the right answer:
>> you can just tack an ORDER BY random() clause onto the query.
> 
> Thank you kind sir
> 
> Cheers
> 
> Mike
> 
>> 
>> -- -- Christophe Pettus x...@thebuild.com
>> 
> 

Peter of the Norse
rahmc...@radio1190.org



-- 
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+unsubscr...@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.




Re: How to get ten random records

2013-03-19 Thread frocco

I am doing this.

 featured = Product.objects.filter(is_featured=True, 
is_active=True).order_by('?')[:4]
  
On Tuesday, March 19, 2013 12:03:28 AM UTC-4, Mike Dewhirst wrote:
>
> 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+unsubscr...@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.




Re: How to get ten random records

2013-03-18 Thread Mike Dewhirst

On 19/03/2013 3:29pm, Christophe Pettus wrote:


On Mar 18, 2013, at 9:03 PM, Mike Dewhirst wrote:


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


This is a case where the raw SQL interface might be the right answer:
you can just tack an ORDER BY random() clause onto the query.


Thank you kind sir

Cheers

Mike



-- -- Christophe Pettus x...@thebuild.com



--
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+unsubscr...@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.




Re: How to get ten random records

2013-03-18 Thread Christophe Pettus

On Mar 18, 2013, at 9:03 PM, Mike Dewhirst wrote:

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

This is a case where the raw SQL interface might be the right answer: you can 
just tack an ORDER BY random() clause onto the query.

--
-- Christophe Pettus
   x...@thebuild.com

-- 
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+unsubscr...@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.




How to get ten random records

2013-03-18 Thread Mike Dewhirst
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+unsubscr...@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.