Reputation: 18929
I have a form:
class ProjectInfoForm(forms.Form):
module = forms.ModelChoiceField(
queryset=Module.objects.all(),
)
piece = forms.ModelChoiceField(
queryset=Piece.objects.all(),
required=False,
)
The second field is populated with option from the first using ajax. However, instantiating it like this is not very efficient as it means the second field is populated on page load uneccessarily (not to mention populates the field before it should).
So I tried changing it to:
...
piece = forms.ModelChoiceField(
queryset=Piece.objects.none(),
required=False,
)
I get the desired result but of course the form does not validate has it has no choices to check against.
Is there some way I can validate the form without populating it or even better validate the two fields together as related models?
Any help much appreciated.
Upvotes: 0
Views: 122
Reputation: 55293
I think the easier way is just to roll your own. You'll just need:
ChoiceField
. Using coerce
will let you transform IDs into objects transparentlyPiece
s for a given Module
.Module
and the Piece
correspondUpvotes: 1