djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

Behaviour when null=True really sucks #69

Closed nemesifier closed 10 years ago

nemesifier commented 10 years ago

When the null attribute is set to True in a DictionaryField, the behaviour sucks.

Because the default value of the dictionary becomes None, trying to setting or getting keys from the dictionary returns a TypeError exception:

set example:

n.data['test'] = 'test'

get example:

n.data.get('test', 'default value')

These two lines both generate an exception.

To mitigate this, one would always have to do something like:

n.data = n.data or {}
n.data['test'] = 'test'

or:

try:
    n.data['test'] = 'test'
except TypeError:
    n.data = {}
    n.data['test'] = 'test'

which generates a lot of redundant, useless, ugly code which also slows down development.

One solution could be to not use null=True at all, but at the present moment the only way i could find to add an hstore field to an existing database table is setting the default value to NULL, which makes possible to add the field in the table but renders development clunky.

I propose that the fallback value of an HStoreField must always be an empty HStoreDict, even when null values are allowed.

nemesifier commented 10 years ago

https://github.com/djangonauts/django-hstore/releases/tag/1.3.3