This works for me.
My model:
class BannerPosition(models.Model):
position = models.SmallIntegerField()
banner = models.ForeignKey(BannerItem)
class Meta:
ordering = ('position', 'banner')
def __str__(self):
return '%s - %s' % (self.banner.name, self.position)
class Admin:
pass
which is sorted via Class Meta.
than my view:
import random
def banner(request, object_id):
"""
get the banners check if random or sorted.
"""
object = Banner.objects.select_related().get(id=object_id)
objectMain = object
object = object.banners.all()
object = list(object)
if objectMain.randomize:
random.shuffle(object)
return render_to_response('playlist.xml', {'object': object})
it checks if the "randomize" field is set in the banner object., if so
it takes the list (you need to convert it to a list for shuffle to
work) and shuffles it. than passes it on. works fine for me.
o
On Oct 1, 6:29 pm, "Alessandro Ronchi" <[EMAIL PROTECTED]>
wrote:
> I tried to
reate a random view with the code on the bottom. It
> doesn't work, the order is always the same.
> What's wrong?
>
> # Create your views here.
>
> from adottami.adozioni.models import Scheda, Referente
> from django.views.generic.list_detail import object_list
> #from django.views.generic.list_detail import object_detail
>
> def random(request, provincia, numero, tipo):
> items = Scheda.objects.all()
> if provincia != None:
> items.filter(provincia__iexact=provincia)
>
> if tipo != None:
> items.filter(tipo__iexact=tipo)
>
> # Ordino a caso
> items.order_by('?')
>
> if numero != None:
> items = items[:numero]
>
> template = "random.html"
>
> return object_list(
> request,
> queryset = items,
> template_name=template,
> )
>
> --
> Alessandro Ronchi
> Skype: aronchihttp://www.alessandroronchi.net- Il mio sito
> personalehttp://www.soasi.com- Sviluppo Softtware e Sistemi Open Source
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---