[Django] #33155: ModelChoiceIteratorValue is not hashable

110 views
Skip to first unread message

Django

unread,
Sep 29, 2021, 1:30:29 PM9/29/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
aljazkosir |
Type: Bug | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: | ModelChoiceIteratorValue
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Recently I migrated from Django 3.0 to Django 3.1. In my code, I add
custom `data-*` attributes to the select widget options. After the upgrade
some of those options broke. Error is `{TypeError}unhashable type:
'ModelChoiceIteratorValue'`.

Example (this one breaks):
{{{
def create_option(self, name, value, label, selected, index,
subindex=None, attrs=None):
context = super().create_option(name, value, label, selected,
index, subindex, attrs)
if not value:
return context
if value in self.show_fields: # This is a dict {1: ['first_name',
'last_name']}
context['attrs']['data-fields'] =
json.dumps(self.show_fields[value])
}}}

However, working with arrays is not an issue:
{{{
def create_option(self, name, value, label, selected, index,
subindex=None, attrs=None):
context = super().create_option(name, value, label, selected,
index, subindex, attrs)
if not value:
return context
if value in allowed_values: # This is an array [1, 2]
...
}}}

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

Django

unread,
Sep 29, 2021, 2:37:56 PM9/29/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* type: Bug => Cleanup/optimization
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Thanks for the ticket. Agreed, we could make `ModelChoiceIteratorValue`
hashable by adding:
{{{
def __hash__(self):
return hash(self.value)
}}}
For now you can use `value.value` as
[https://docs.djangoproject.com/en/3.2/releases/3.1/#id1 documented in the
"Backwards incompatible changes in 3.1" section].

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

Django

unread,
Sep 29, 2021, 2:44:30 PM9/29/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Aljaž Košir):

Replying to [comment:1 Mariusz Felisiak]:


> Thanks for the ticket. Agreed, we could make `ModelChoiceIteratorValue`
hashable by adding:
> {{{
> def __hash__(self):
> return hash(self.value)
> }}}
> For now you can use `value.value` as
[https://docs.djangoproject.com/en/3.2/releases/3.1/#id1 documented in the

"Backwards incompatible changes in 3.1" section]. Would you like to
prepare a patch?


Yes, sure.

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

Django

unread,
Sep 29, 2021, 2:44:37 PM9/29/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
Type: | Košir
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aljaž Košir):

* owner: nobody => Aljaž Košir
* status: new => assigned


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

Django

unread,
Sep 29, 2021, 6:01:01 PM9/29/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
Type: | Košir
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aljaž Košir):

* has_patch: 0 => 1


Comment:

Patch: https://github.com/django/django/pull/14915

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

Django

unread,
Sep 30, 2021, 12:46:26 AM9/30/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
Type: | Košir
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1


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

Django

unread,
Sep 30, 2021, 4:29:35 AM9/30/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
Type: | Košir
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aljaž Košir):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/33155#comment:6>

Django

unread,
Sep 30, 2021, 4:29:53 AM9/30/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
Type: | Košir
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
ModelChoiceIteratorValue |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aljaž Košir):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/33155#comment:7>

Django

unread,
Sep 30, 2021, 5:30:56 AM9/30/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
| Košir
Type: Bug | Status: assigned
Component: Forms | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
ModelChoiceIteratorValue | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* type: Cleanup/optimization => Bug
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33155#comment:8>

Django

unread,
Sep 30, 2021, 6:08:02 AM9/30/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
| Košir
Type: Bug | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
ModelChoiceIteratorValue | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7b8beeee3d45cafd7bec7ff3ee0e4371e142c36d" 7b8beee]:
{{{
#!CommitTicketReference repository=""
revision="7b8beeee3d45cafd7bec7ff3ee0e4371e142c36d"
Fixed #33155 -- Made ModelChoiceIteratorValue instances hashable.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33155#comment:9>

Django

unread,
Sep 30, 2021, 6:09:50 AM9/30/21
to django-...@googlegroups.com
#33155: ModelChoiceIteratorValue is not hashable.
-------------------------------------+-------------------------------------
Reporter: Aljaž Košir | Owner: Aljaž
| Košir
Type: Bug | Status: closed
Component: Forms | Version: 3.1

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
ModelChoiceIteratorValue | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"0a49276065575867fbbf279f138b399c535e3b6d" 0a492760]:
{{{
#!CommitTicketReference repository=""
revision="0a49276065575867fbbf279f138b399c535e3b6d"
[4.0.x] Fixed #33155 -- Made ModelChoiceIteratorValue instances hashable.

Backport of 7b8beeee3d45cafd7bec7ff3ee0e4371e142c36d from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33155#comment:10>

Reply all
Reply to author
Forward
0 new messages