Opened 7 years ago
Last modified 7 years ago
#28443 closed Bug
MultiValueField does not work with ModelChoiceField? — at Initial Version
Reported by: | adam-kral | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The code: (where AddressInput is a MultiWidget)
class AddressFormField(MultiValueField): widget = AddressInput def __init__(self, queryset, empty_label="---------", to_field_name=None, limit_choices_to=None, *args, **kwargs): fields = ( ModelChoiceField(queryset, empty_label=empty_label, to_field_name=to_field_name, limit_choices_to=limit_choices_to, *args, **kwargs), CharField() ) super().__init__(fields=fields, require_all_fields=False, *args, **kwargs) #self.widget.choices = self.fields[0].widget.choices #### if not commented out, I get another error: AttributeError: 'RelatedFieldWidgetWrapper' object has no attribute 'decompress' def compress(self, data_list): if not data_list[1]: return None if not data_list[0]: raise ValidationError('Invalid address') return data_list[0] class AddressForeignKey(ForeignKey): def formfield(self, **kwargs): # This is a fairly standard way to set up some defaults # while letting the caller override them. defaults = {'form_class': AddressFormField} defaults.update(kwargs) return super().formfield(**defaults)
I get this error: AttributeError: 'AddressInput' object has no attribute 'choices'
Because ModelChoiceField did not declare it. Passing the widget to ModelChoiceField does not work as it makes a copy if it's an instance.
Thus I set the choices attribute manually as you can see in the commented out code.
But then I got another error which I didn't resolve: AttributeError: 'RelatedFieldWidgetWrapper' object has no attribute 'decompress'