Opened 8 years ago

Closed 8 years ago

#27582 closed Cleanup/optimization (fixed)

Allow HStoreField to store null values in key: value pairs

Reported by: David Hoffman Owned by: Tim Graham <timograham@…>
Component: contrib.postgres Version: dev
Severity: Normal Keywords: hstore hstorefield
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Overview

PostgreSQL allows values to be either strings or nulls in hstore: https://www.postgresql.org/docs/9.5/static/hstore.html

It appears that the form and model HStoreFields do not support null values. Is this something that should be fixed?

How to reproduce for the form field

>>> from django.contrib.postgres import forms
>>> field = forms.HStoreField()
>>> field.clean('{"a": null}')
{u'a': u'None'} 

Expected Behavior

The null has been converted to a string, when it should actually be the None value

How to reproduce for the model field

>>> from django.contrib.postgres.fields import HStoreField
>>> field = HStoreField()
>>> field.clean({'a': None}, None)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/dhoffman/.envs/brain-campaign/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 588, in clean
    self.validate(value, model_instance)
  File "/Users/dhoffman/.envs/brain-campaign/lib/python2.7/site-packages/django/contrib/postgres/fields/hstore.py", line 36, in validate
    params={'key': key},
ValidationError: [u'The value of "a" is not a string.']

Expected Behavior

This should actually be allowed without error.

Change History (7)

comment:1 by David Hoffman, 8 years ago

Status: assignednew
Summary: PostgreSQL form and model HStoreFields do not support null valuesPostgreSQL form and model HStoreFields do not support writing null values

comment:2 by David Hoffman, 8 years ago

I have opened a pull request at https://github.com/django/django/pull/7668

comment:3 by Tim Graham, 8 years ago

Cc: Marc Tamlyn added

Marc, did you make a conscious design decision about this when you first implemented the field?

comment:4 by Marc Tamlyn, 8 years ago

Not that I recall. This use case should be valid.

comment:5 by Tim Graham, 8 years ago

Cc: Marc Tamlyn removed
Has patch: set
Patch needs improvement: set
Summary: PostgreSQL form and model HStoreFields do not support writing null valuesAllow HStoreField to store null values in key: value pairs
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

comment:6 by Tim Graham, 8 years ago

Patch needs improvement: unset

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

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In bf84d04:

Fixed #27582 -- Allowed HStoreField to store null values.

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