Opened 2 years ago

Closed 4 months ago

#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: master
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 Tim Graham)

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 (17)

comment:1 Changed 2 years ago by Tim Graham

Component: Uncategorizedcontrib.postgres
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 Changed 2 years ago by PREMANAND

Owner: changed from nobody to PREMANAND
Status: newassigned

comment:9 Changed 2 years ago by PREMANAND

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.

comment:10 Changed 2 years ago by Tim Graham

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 Changed 2 years ago by PREMANAND

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)
Last edited 2 years ago by PREMANAND (previous) (diff)

comment:12 Changed 2 years ago by Tim Graham

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:14 Changed 2 years ago by Claude Paroz

Description: modified (diff)

Description restored.

comment:15 Changed 2 years ago by Meiyer

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)
Last edited 2 years ago by Meiyer (previous) (diff)

comment:17 Changed 2 years ago by Tim Graham

Component: contrib.postgresForms
Description: modified (diff)

I believe it affects all inline formsets, not just those in the admin. There's a PR without tests.

comment:18 Changed 10 months ago by felixxm

Owner: changed from PREMANAND to felixxm
Version: 1.9master

comment:19 Changed 7 months ago by felixxm

Owner: felixxm deleted
Status: assignednew

comment:20 Changed 7 months ago by Demur Nodia

Owner: set to Demur Nodia
Status: newassigned

comment:22 Changed 7 months ago by Demur Nodia

Has patch: set

comment:23 Changed 5 months ago by Carlton Gibson

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 Changed 5 months ago by Demur Nodia

Patch needs improvement: unset

comment:25 Changed 4 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 06a11ef:

Fixed #26819 -- Fixed BaseModelFormSet.validate_unique() "unhashable type: list" crash.

Note: See TracTickets for help on using tickets.
Back to Top