The two ways are pretty much the same, one picks a random number to use for a slice, the other picks a random element in a queryset. I would go for whatever you think is the easiest to understand, when browsing through the code.
Also, if you're not sure that atleast 3 elements will come from the query you would need an if statement in the randrange version aswell. ~Jakob On 15 Apr., 17:54, Baxter <mail.bax...@gmail.com> wrote: > On Apr 15, 10:44 am, google torp <toppe...@gmail.com> wrote:> What I meant > was that you could do it like this > > > import random > > sa = Article.objects.filter(...).order_by('-id') > > random_choice = sa[random.randrange(2, len(sa))] > > Thanks Jakob. Is there an advantage to doing it that way rather than > what I came up with, using choice() rather than random? --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---