Devendra Kumar
Devendra Kumar

Reputation: 21

how to shuffle list in django views.py?

i'm trying to shuffle a list in django.views

views.py

import random

def all_songs( request):
    songs_list = Songs.objects.all()
    songs_list=random.shuffle(songs_list)

but after entering this code the error showing "'QuerySet' object does not support item assignment" shows up. how do i do it without item assignment ?

Upvotes: 1

Views: 2973

Answers (2)

Devendra Kumar
Devendra Kumar

Reputation: 21

convert list(here song_list) to list and then shuffle it....

def all_songs( request):
    songs_list = list(Songs.objects.all())
    random.shuffle(songs_list)

no more queryset error

Upvotes: -2

sax
sax

Reputation: 3806

Songs.objects.order_by('?')

returns a randomly ordered queryset

see docs here

Upvotes: 6

Related Questions