Hi list, Considering this schema (below) I'm trying to figure out how to create a ModelForm which links Actors to Films (all known actors in a multi- select widget). I can't seem to figure out how to do this in a way that ModelForm will do the heavy lifting.
I would like to make a page (e.g. /film/add_actors/123) that has a multi select widget which allows me to select one or more Actors (from a list of all possible actors) and link them to the given film object. Is ModelForm the right thing to use here? I'm going to outline what I've tried to give context. If I simply do this: class FilmRoleForm(forms.ModelForm): class Meta: model = FilmRole fields = (('actor'),) Then it only gives me a form to populate one row in FilmRole (I know I have to manually munge the other fields but I can handle that). That behaviour makes sense. However, it isn't the behaviour I want. I want to be able to populate many rows at once (since a film can have many actors). So I tried this: class FilmRoleForm(forms.ModelForm): actor = forms.ModelMultipleChoiceField( widget=forms.SelectMultiple, queryset = Actor.objects.all() ) class Meta: model = FilmRole fields = (('actor'),) ok - progress. Now I get the widget I want (a multi select list) but when I submit it I get an exception like this: Cannot assign "[<Actor: Clint Eastwood>, <Actor: Al Pacino>]": "FilmRole.actor" must be a "Actor" instance. Again, I can see why this is but it isn't the behaviour I want. How can I tell Django "use a multi-select widget and I expect a list of values not a single (non list) value". Assuming I were able to circumvent that problem, I'm also trying to avoid having to write code like this in the view: for actor in form.cleaned_data.get('actor'): FilmRole.get_or_create(actor = actor, film = film) ... that isn't so bad but it also requires that I keep track of rows I need to delete (those FilmRole objects in the database from previous submits that have been unselected in the latest form.cleaned_data.get('actor')) I'd just like all that behaviour be implicit somehow. I know that when my mapping table (FilmRole) is implicitly created and doesn't have extract fields then Django can handle all that magically. Is there some other variant of ModelForm that can handle this? Any suggestions are most welcome. Thanks. ############################# class Actor(models.Model): name = models.CharField(max_length=255) dob = models.DateField() def __unicode__(self): return self.name class Film(models.Model): title = models.CharField(max_length=255) actors = models.ManyToManyField(Actor, through = 'FilmRole') released = models.DateField() def __unicode__(self): return self.title class FilmRole(models.Model): film = models.ForeignKey(Film) actor = models.ForeignKey(Actor) date_joined = models.DateField() earnings = models.IntegerField() class Meta: unique_together = ("film", "actor") -- 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.