On May 19, 2006, at 2:44 PM, [EMAIL PROTECTED] wrote: > I want to add a random picture component to an album app I'm building. > So I have a class Picture. I want to choose ten to twenty random > pictures out of all pictures submitted. > Obviously, I don't want to load all Picture instances (potentially > lots!) into memory just to do a random selection of a few. > > So, in pure SQL I would expect to just fetch a list of IDs, using a > couple of search criteriam, whittle that down to my list of ten random > IDs and then fetch just those objects. I haven't found a "get_ids()" > method or similar, mirroring get_list() (including the selection > criteria), which would allow me to do this.
There's a much simplier way:: Photo.objects.order_by('?')[:10] (Which corresponds roughly to this SQL: ``SELECT * FROM photos ORDER BY RANDOM() LIMIT 10``) Yes, it is that easy. Jacob --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---