#26819 closed Bug (fixed)
Using a ArrayField on Meta.unique_together throws "unhashable type: 'list'" on validate_unique method
Reported by: | Gleber Diniz | Owned by: | Demur Nodia |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
It happens on a second save (when there is already data to be validated)
On this line: "if row_data in seen_data:", because row_data contains a list and seen_data is a set (row_data: (1, [1, 1]) / seen_data: set())
Example to reproduce:
models:
class Map(models.Model): name = models.CharField('name', max_length=128) class MapSpot(models.Model): map = models.ForeignKey('body.Map', related_name='spots') position = ArrayField(models.IntegerField(), size=2) class Meta: unique_together = [('map', 'position')]
admin:
class MapSpotInline(admin.TabularInline): model = MapSpot extra = 0 @admin.register(Map) class MapAdmin(admin.ModelAdmin): inlines = [MapSpotInline]
Change History (19)
comment:1 by , 8 years ago
Component: | Uncategorized → contrib.postgres |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 8 years ago
comment:10 by , 8 years ago
Not sure without looking into the details myself, but it might be better if the validate_unique()
method took care of casting as needed since changing the return value of to_python()
could be backwards-incompatible.
comment:11 by , 8 years ago
Description: | modified (diff) |
---|
As you mentioned, implemented fix in validate_unique() method. The above code fixes the issue and all the unit tests got passed.
The only problem is Im not sure how to write the test case. It needs to be simulated as if a user creates a form in the html page. Do you have any pointers for testing similar condition?
row_data = tuple(d._get_pk_val() if hasattr(d, '_get_pk_val') else tuple(d) if type(d) is list else d for d in row_data)
comment:12 by , 8 years ago
Take a look at tests/postgres_tests/test_array.py
for the existing tests for ArrayField
.
By the way, you inadvertently deleted the ticket's description in your last comment. Can you restore it? Deleting your comment should undo that action if you can't find another way.
comment:15 by , 8 years ago
This happens only for Inline admin models (only validate_unique()
of BaseModelFormSet
contains the problematic code).
This is not limited to ArrayField
; any custom field may be affected. In our case, the phonenumber_field.modelfields.PhoneNumberField
is causing the error unhashable type: 'PhoneNumber'
. Since Django documentation does not mandate that custom fields are hashable (i.e., define the __hash__()
method), I suppose that most of the existing non-builtin fields are affected.
Looks like the solution is either to keep seen_data
as a set()
and rely on some property of the field (unreliable and error-prone, see sample code below), or give up set()
and manually implement removal of duplicates.
row_data = tuple(d._get_pk_val() if hasattr(d, '_get_pk_val') else d if d.__hash__ is not None else d.__repr__() for d in row_data)
comment:17 by , 8 years ago
Component: | contrib.postgres → Forms |
---|---|
Description: | modified (diff) |
I believe it affects all inline formsets, not just those in the admin. There's a PR without tests.
comment:18 by , 7 years ago
Owner: | changed from | to
---|---|
Version: | 1.9 → master |
comment:19 by , 7 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:20 by , 7 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:21 by , 7 years ago
Prepared fix for the issue during DjangoCon Europe 2018 sprints
PR: https://github.com/django/django/pull/9993
Used suggestions from previous work:
comment:22 by , 7 years ago
Has patch: | set |
---|
comment:23 by , 6 years ago
Patch needs improvement: | set |
---|
Patch looks good; there were just a few adjustments needed (Review on PR).
Please uncheck Patch needs improvement when they're addressed and we'll get this in.
Thanks Demur!
comment:24 by , 6 years ago
Patch needs improvement: | unset |
---|
comment:26 by , 5 years ago
Although this has been fixed specifically for ArrayField, we still get this error with JSONField. Probably more generic solution needed.
comment:27 by , 5 years ago
Ivan, please open a new ticket with the minimal reproduce example. Thanks.
This issue occurs as the array item is stored in the lists and there is a mis-match while comparison.
The arrayitem is stored in row_data as list and list are not hashable as the error says.
if row_data in seen_data:
This issue can be fixed by changing this line ie; return a tuple instead of list.
https://github.com/PREM1980/django/blob/master/django/contrib/postgres/forms/array.py#L56
Please review and provide your comments.