[Django] #28443: MultiValueField does not work with ModelChoiceField?

4 views
Skip to first unread message

Django

unread,
Jul 27, 2017, 3:31:53 PM7/27/17
to django-...@googlegroups.com
#28443: MultiValueField does not work with ModelChoiceField?
-----------------------------------------+------------------------
Reporter: adam-kral | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
The code: (where AddressInput is a MultiWidget)

{{{#!python
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'

--
Ticket URL: <https://code.djangoproject.com/ticket/28443>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 27, 2017, 3:34:03 PM7/27/17
to django-...@googlegroups.com
#28443: MultiValueField does not work with ModelChoiceField?
---------------------------+--------------------------------------
Reporter: adam-kral | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by adam-kral):

* type: Uncategorized => Bug
* component: Uncategorized => Forms


--
Ticket URL: <https://code.djangoproject.com/ticket/28443#comment:1>

Django

unread,
Jul 27, 2017, 4:23:08 PM7/27/17
to django-...@googlegroups.com
#28443: MultiValueField does not work with ModelChoiceField?
---------------------------+--------------------------------------
Reporter: adam-kral | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => needsinfo


Comment:

It's not immediately obvious to me whether or not Django is at fault and
the ticket tracker isn't the place to get debugging help. Although I can't
say for sure since you didn't provide code for `AddressInput`, it looks
more likely that you haven't constructed the widget correctly. Please see
TicketClosingReasons/UseSupportChannels for ways to get help and reopen
the ticket if you confirm a bug. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/28443#comment:2>

Django

unread,
Jul 28, 2017, 4:24:00 AM7/28/17
to django-...@googlegroups.com
#28443: MultiValueField does not work with ModelChoiceField?
---------------------------+--------------------------------------
Reporter: adam-kral | Owner: nobody

Type: Bug | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Description changed by adam-kral:

Old description:

New description:

The code: (where AddressInput is a MultiWidget)

{{{#!python
class AddressInput(widgets.MultiWidget):
def __init__(self, attrs=None):
self.widgets = widgets.HiddenInput(attrs),
widgets.TextInput(attrs). # note that the second widget would be
customized, I'm only providing simplified example, which does produce the
same error
super().__init__(self.widgets, attrs)

def decompress(self, value):
try:
address = AddressPoint.objects.get(pk=value)
except (AddressPoint.DoesNotExist, ValueError):
address = None

return [value, str(address)]

def value_from_datadict(self, data, files, name):
return tuple(widget.value_from_datadict(data, files,
f'{name}_{i}') for i, widget in enumerate(self.widgets))

super().__init__(fields=fields, require_all_fields=False, *args,
**kwargs)

return data_list[0]

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28443#comment:3>

Django

unread,
Jul 28, 2017, 4:25:04 AM7/28/17
to django-...@googlegroups.com
#28443: MultiValueField does not work with ModelChoiceField?
---------------------------+--------------------------------------
Reporter: adam-kral | Owner: nobody

Type: Bug | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Description changed by adam-kral:

Old description:

> The code: (where AddressInput is a MultiWidget)
>
> {{{#!python


> class AddressInput(widgets.MultiWidget):
> def __init__(self, attrs=None):
> self.widgets = widgets.HiddenInput(attrs),
> widgets.TextInput(attrs). # note that the second widget would be
> customized, I'm only providing simplified example, which does produce the
> same error
> super().__init__(self.widgets, attrs)
>
> def decompress(self, value):
> try:
> address = AddressPoint.objects.get(pk=value)
> except (AddressPoint.DoesNotExist, ValueError):
> address = None
>
> return [value, str(address)]
>
> def value_from_datadict(self, data, files, name):
> return tuple(widget.value_from_datadict(data, files,
> f'{name}_{i}') for i, widget in enumerate(self.widgets))
>

> class AddressFormField(MultiValueField):

New description:

The code: (where AddressInput is a MultiWidget)

{{{#!python


class AddressInput(widgets.MultiWidget):
def __init__(self, attrs=None):
self.widgets = widgets.HiddenInput(attrs),

widgets.TextInput(attrs) # note that the second widget would be


customized, I'm only providing simplified example, which does produce the
same error
super().__init__(self.widgets, attrs)

def decompress(self, value):
try:
address = AddressPoint.objects.get(pk=value)
except (AddressPoint.DoesNotExist, ValueError):
address = None

return [value, str(address)]

def value_from_datadict(self, data, files, name):
return tuple(widget.value_from_datadict(data, files,
f'{name}_{i}') for i, widget in enumerate(self.widgets))

super().__init__(fields=fields, require_all_fields=False, *args,
**kwargs)

return data_list[0]

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28443#comment:4>

Django

unread,
Jul 28, 2017, 4:42:40 AM7/28/17
to django-...@googlegroups.com
#28443: MultiValueField does not work with ModelChoiceField?
---------------------------+--------------------------------------
Reporter: adam-kral | Owner: nobody

Type: Bug | Status: closed
Component: Forms | Version: 1.11
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Description changed by adam-kral:

Old description:

> The code: (where AddressInput is a MultiWidget)
>
> {{{#!python


> class AddressInput(widgets.MultiWidget):
> def __init__(self, attrs=None):
> self.widgets = widgets.HiddenInput(attrs),
> widgets.TextInput(attrs) # note that the second widget would be
> customized, I'm only providing simplified example, which does produce the
> same error
> super().__init__(self.widgets, attrs)
>
> def decompress(self, value):
> try:
> address = AddressPoint.objects.get(pk=value)
> except (AddressPoint.DoesNotExist, ValueError):
> address = None
>
> return [value, str(address)]
>
> def value_from_datadict(self, data, files, name):
> return tuple(widget.value_from_datadict(data, files,
> f'{name}_{i}') for i, widget in enumerate(self.widgets))
>

> class AddressFormField(MultiValueField):

New description:

The code: (where AddressInput is a MultiWidget)

{{{#!python


class AddressInput(widgets.MultiWidget):
def __init__(self, attrs=None):
self.widgets = widgets.HiddenInput(attrs),
widgets.TextInput(attrs) # note that the second widget would be
customized, I'm only providing simplified example, which does produce the
same error
super().__init__(self.widgets, attrs)

def decompress(self, value):
try:
address = AddressPoint.objects.get(pk=value)
except (AddressPoint.DoesNotExist, ValueError):
address = None

return [value, str(address)]


super().__init__(fields=fields, require_all_fields=False, *args,
**kwargs)

return data_list[0]

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28443#comment:5>

Reply all
Reply to author
Forward
0 new messages