views:

16

answers:

1

Say that Blog model has a one-to-many relationship with Entry model. In a form, is there an elegant way display the set of Entries for a Blog instance as a list of checkboxes, so the user may select and process some/all of the Entries?

+1  A: 

Sure.

class BlogForm(forms.ModelForm):
    entries = forms.MultipleChoiceField(queryset=Entry.objects.all(),
                                        widget=forms.CheckboxSelectMultiple)

    class Meta:
        model = Blog

    def __init__(self, *args, **kwargs):
        super(BlogForm, self).__init__(*args, **kwargs)
        if self.instance:
            entries = Entry.objects.filter(blog=blog)
            self.fields['entries'].queryset = entries
Daniel Roseman