Using a backwards relation (i.e FOO_set) for ModelChoiceField in Django
- by Bwmat
I have a model called Movie, which has a ManyToManyField called director to a model called Person, and I'm trying to create a form with ModelChoiceField like so:
class MovieSearchForm(forms.Form):
producer = forms.ModelChoiceField(label='Produced by',
queryset=movies.models.Person.producer_set,
required=False)
but this seems to be failing to compile (I'm getting a ViewDoesNotExist exception for the view that uses the form, but it goes away if I just replace the queryset with all the person objects), I'm guessing because '.producer_set' is being evaluated too 'early'. How can I get this work?
here are the relevant parts of the movie/person classes:
class Person(models.Model):
name = models.CharField(max_length=100)
class Movie(models.Model):
...
producer = models.ForeignKey(Person, related_name="producers")
director = models.ForeignKey(Person, related_name="directors")
What I'm trying to do is get ever Person who is used in the producer field of some Movie.