views:

570

answers:

1

I want to make (ordered) Song list form.

models.py

class Song(models.Model):
    title = models.CharField(max_length=60)

class List(models.Model):
    title = models.CharField(max_length=100)
    songs = models.ManyToManyField(Song, through='Order')

class Order(models.Model):
    list = models.ForeignKey(List)
    song = models.ForeignKey(Song)
    order = models.IntegerField(unique=True)

and,

forms.py

class ListEditForm(forms.Form):
    title = forms.CharField(
        label='List Title',
        widget=forms.TextInput(attrs={'size':100})
    )
    songs = forms.ModelMultipleChoiceField(
        label= 'Song Order',
        required= False,
        widget=forms.CheckboxSelectMultiple,
        queryset= Song.objects.none()
    )

and then,

ListEditForm.py

<form id="edit-form" method="post" action="/list/edit/">
<p>
    {{ form.title.errors }}
    {{ form.title.label_tag }}
    {{ form.title }}
</p>
<p>
    {% for song in form.songs %}
     {{ song.label_tag }}
     {{ song }}
    {% endfor %}
</p>
<input type="submit" value="save" />
</form>

This template raises following error: Caught an exception while rendering: 'BoundField' object is not iterable

How do I split render each form field in 'form.songs'?

+1  A: 

You're getting a not iterable error because ListEditForm.songs is a single field that contains a list of song choices, rather than a list of individual song fields. From your description I'm not sure how you wanted the list to be rendered.

I would suggest looking into using a Django formset. http://docs.djangoproject.com/en/dev/topics/forms/formsets/

moberley
Thanks for your reply.Sorry for my broken english.