On Fri, Mar 4, 2011 at 1:08 PM, werefr0g <weref...@yahoo.fr> wrote: > On Thu, Mar 3, 2011 at 11:50 AM, werefr0g <weref...@yahoo.fr> wrote: > > Hello, > > Sorry if I misunderstand, but what is wrong about using a ModelForm on your > Film bounded to the film instance? > > My bad... how did I missed the intermediary model involved in the ManyToMany > relashionship. Sorry. > > When I tried that I kept running into this error: > "Cannot set values on a ManyToManyField which specifies an > intermediary model. Use foo.FilmRole's Manager instead. > > As you are using an intermediary model, you must insert multiple FilmRole, > one for each actor to this film, but you don't want using a formset, do > you?. > > Is the following going toward your goal? > > # forms.py > class FilmActorsForm(forms.Form): > actors = forms.ModelMultipleChoiceField(queryset=Actors..., > required=...) > > # views.py > def assign_actors(film_id): > film = get_object_or_404(Film, id=film_id) > if request.method == 'POST': > form = FilmActorsForm(request.POST) > if form.is_valid(): > chosen_actors = form.cleaned_data['actors'] > current_actors = film.actors.all() > # handle deletion / creation of filmroles > return HttpResponseRedirect(...) > else: > form = FilmActorForm()
Thanks. This is pretty close to what I'm doing. However, I'd like to not have to write all the associated boiler-plate (i.e. I have to figure out if any film/actor relationships have been deleted since the last save and explicitly write code to remove those and so on). It seems that I would even have this problem if I added a simple "auto now" date/time field to my relationship class, which seems a bit restrictive. ModelForm already knows how to do all that (it can do so when the relationship class is implicitly created) so what I imagined was *somewhere* I could override a method and provide the defaults for the missing fields and then it would all just works magically. Does this make sense? Can anyone give me any pointers to how I might do this (I'm keen to learn the "right way" to do things). I'm happy to hack this myself and contribute something if someone could give me some pointers of where I should start or where something like this ought to live. Thanks. -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.