Using a backwards relation (i.e FOO_set) for ModelChoiceField in Django
Posted
by
Bwmat
on Stack Overflow
See other posts from Stack Overflow
or by Bwmat
Published on 2012-07-11T07:15:22Z
Indexed on
2012/07/11
15:15 UTC
Read the original article
Hit count: 215
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.
© Stack Overflow or respective owner