Wednesday, June 29, 2011

Vs: how to override a ModelChoiceField in order to add FKs directly in a TextInput?

Since ModelChoiceField validates that the given id exists in the queryset, there can be no valid choices when you use MyRelatedClass.objects.none().

Try MyRelatedClass.objects.all() or filter by some attribute. If the queryset depends on some user input or conditions you can use something like:

def __init__(self, some_value, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)
    qset= MyRelatedClass.objects.filter(some_field=some_value)
    self.fields['myrelation '].queryset = qset


You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment