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


Martin

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Cm7-6sNIfeMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment