class Edittalkform(forms.Form): def __init__(self, *args, **kwargs): self.fields['speaker'].choices = [(chld.id, chld.username) for chld in Delegate.objects.all()] title = forms.CharField(max_length=250, label=_("Talk Title") ) speaker = forms.MultipleChoiceField(label=_("Speaker(s)"), choices=() ) @user_passes_test(lambda u: u.is_anonymous()==False ,login_url="/web/login/") @transaction.commit_on_success def edittalk(request,id): if request.POST: form = Edittalkform(request.POST) if form.is_valid(): fm = form.cleaned_data delgid = Delegate.objects.get(username=request.user).id if str(delgid) not in fm['speaker']: form.errors['speaker'] = [_("Your own username\ is missing from the list of speakers")] if not form.errors: newtalk.title = fm['title'] newtalk.save() # remove all the previous speakers newtalk.speaker.clear() for spk in fm['speaker']: newtalk.speaker.add(Delegate.objects.get(pk=int(spk))) return HttpResponseRedirect("/web/talks/" ) else: tlk = Talk.objects.get(pk=id) data = { 'title': tlk.title, 'speaker': [p.id for p in tlk.speaker.all()], } form = Edittalkform(data) return render_to_response("web/edittalk.html", {'form':form, 'request':request })