Account for initial data when binding a DynamicModelChoiceField

This commit is contained in:
Jeremy Stretch 2020-02-11 10:21:44 -05:00
parent 221805a63e
commit fb56d5bc66

View File

@ -562,10 +562,11 @@ class DynamicModelChoiceField(forms.ModelChoiceField):
# Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options # Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options
# will be populated on-demand via the APISelect widget. # will be populated on-demand via the APISelect widget.
field_name = '{}{}'.format(self.to_field_name or 'pk', self.field_modifier)
if bound_field.data: if bound_field.data:
field_name = '{}{}'.format(self.to_field_name or 'pk', self.field_modifier) self.queryset = self.queryset.filter(**{field_name: bound_field.data})
kwargs = {field_name: bound_field.data} elif bound_field.initial:
self.queryset = self.queryset.filter(**kwargs) self.queryset = self.queryset.filter(**{field_name: bound_field.initial})
else: else:
self.queryset = self.queryset.none() self.queryset = self.queryset.none()