Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26672 closed Bug (fixed)

forms.HStoreField crashes on an empty JSON array

Reported by: Brad Melin Owned by: Brad Melin
Component: contrib.postgres Version: 1.9
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Problem: Submitting a JSON array ([]) into the default Django admin form for HStoreField (django.contrib.postgres.fields.HStoreField) throws an error.

There is some incorrect logic with the validation where if anything but a dict is passed to the to_python method, the field tries to json.load() the data and then iterate over it with value.items(). Lists don't implement this method, and so we an AttibuteError is raised.

To reproduce:

  • Add an HStoreField to a model, and register that model with Django Admin.
  • In the model admin edit view, insert a list into the textfield
  • Submit.

Traceback: https://dpaste.de/bjUh

Change History (12)

comment:1 by Brad Melin, 8 years ago

comment:2 by Brad Melin, 8 years ago

Owner: set to Brad Melin
Status: newassigned

comment:3 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

I suppose a ValidationError should be raised instead.

comment:4 by Tim Graham, 8 years ago

Summary: When adding a JSON array to the Admin HStoreField widget, Django raises an AttibuteErrorforms.HStoreField crashes on an empty JSON array

comment:5 by Brad Melin, 8 years ago

Has patch: set

comment:6 by Simon Charette, 8 years ago

Patch needs improvement: set
Severity: NormalRelease blocker

Marking as release blocker for 1.9 as this is a bug in a new feature with possible data loss on failed submission.

PR needs minor code adjustment and a release note.

comment:7 by Brad Melin, 8 years ago

Patch needs improvement: unset

comment:8 by Brad Melin, 8 years ago

Patch needs improvement: set

comment:9 by Brad Melin, 8 years ago

Patch needs improvement: unset

comment:10 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In f6517a5:

Fixed #26672 -- Fixed HStoreField to raise ValidationError instead of crashing on non-dict JSON input.

comment:11 by Tim Graham <timograham@…>, 8 years ago

In 95ed1514:

[1.9.x] Fixed #26672 -- Fixed HStoreField to raise ValidationError instead of crashing on non-dict JSON input.

Backport of f6517a5335ccc4858ee540548a1bd162bec36c46 from master

comment:12 by Tim Graham <timograham@…>, 8 years ago

In 3eb3186:

[1.10.x] Fixed #26672 -- Fixed HStoreField to raise ValidationError instead of crashing on non-dict JSON input.

Backport of f6517a5335ccc4858ee540548a1bd162bec36c46 from master

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