Opened 8 years ago
Last modified 5 years ago
#26819 closed Bug
Using a ArrayField on Meta.unique_together throws "unhashable type: 'list'" on validate_unique method — at Version 17
Reported by: | Gleber Diniz | Owned by: | PREMANAND |
---|---|---|---|
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 (9)
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 related to the database and is purely a Python issue. 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.
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.