#33155 closed Bug (fixed)
ModelChoiceIteratorValue is not hashable.
Reported by: | Aljaž Košir | Owned by: | Aljaž Košir |
---|---|---|---|
Component: | Forms | Version: | 3.1 |
Severity: | Normal | Keywords: | ModelChoiceIteratorValue |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
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] ...
Change History (10)
follow-up: 2 comment:1 by , 3 years ago
Easy pickings: | set |
---|---|
Summary: | ModelChoiceIteratorValue is not hashable → ModelChoiceIteratorValue is not hashable. |
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:2 by , 3 years ago
Replying to 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 documented in the "Backwards incompatible changes in 3.1" section. Would you like to prepare a patch?
Yes, sure.
comment:3 by , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 3 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
comment:6 by , 3 years ago
Patch needs improvement: | unset |
---|
comment:7 by , 3 years ago
Needs tests: | unset |
---|
comment:8 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|---|
Type: | Cleanup/optimization → Bug |
Thanks for the ticket. Agreed, we could make
ModelChoiceIteratorValue
hashable by adding:For now you can use
value.value
as documented in the "Backwards incompatible changes in 3.1" section.