Oh, and here's the view:

    def edit_participant(request, project_pk, participant_pk):
        project = get_object_or_404(Project, pk = project_pk)
        participant = get_object_or_404(Participant, pk =
participant_pk)

        if request.method == 'POST':
            form = EditParticipantForm(request.POST,
instance=participant)
            if form.is_valid():
                form.save()
                request.session['flash_message'] = 'Participant
successfully updated'
                return HttpResponseRedirect(reverse('view_project',
kwargs={ 'project_pk': project.pk }))
        else:
            form = EditParticipantForm(instance=participant)

        return render_to_response('projects/edit_participant.html',
                              { 'form': form, 'participant':
participant },
 
context_instance=RequestContext(request))



On Dec 23, 9:04 pm, Julien <[EMAIL PROTECTED]> wrote:
> Hi there,
>
> I don't understand why the form doesn't validate when I don't fill out
> a ManyToManyField.
>
> Here's the code:
>
> class Participant(models.Model):
>     project = models.ForeignKey(Project, related_name='participants')
>     roles = models.ManyToManyField(Role, blank=True, null=True)
>     user = models.ForeignKey(User)
>
> class EditParticipantForm(forms.ModelForm):
>     roles =
> forms.MultipleChoiceField(widget=TableSelectMultiple(item_attrs=('name',)))
>     def __init__(self, *args, **kwargs):
>         super(EditParticipantForm, self).__init__(*args, **kwargs)
>         self.fields['roles'].choices = [(role.pk, role) for role in
> Role.objects.all()]
>     class Meta:
>         model = Participant
>         fields = ('roles',)
>
> (The custom widget TableSelectMultiple is taken from 
> there:http://www.djangosnippets.org/snippets/518/)
>
> If I don't select any option for 'roles' I get the error 'This field
> is required'.
> Yet, the doc says that the form field shouldn't be required if the
> corresponding model field has 
> 'blank=True':http://www.djangoproject.com/documentation/modelforms/#field-types
>
> Is that a bug or is there a sensible explanation?   :)
>
> Thanks a lot for your help!
>
> Julien
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to