however - there is a weird bug, If I create a first object, it is created
with an empty dict indeed. but when creating another object, instead of an
empty dictionary - it is loaded with the dictionary of the previous, non-
related object!!. I'm not sure where's the problem in django's code but
maybe it's related to models/base.py, in line 518 there's a "if kwargs"
which returns false in the case of the empty dict that is my default.
maybe it should return something?
my workaround was simple of course - stop using the default value. just
wanted to let you know...
--
Ticket URL: <https://code.djangoproject.com/ticket/28406>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
* type: Uncategorized => Bug
Comment:
[https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.default
As documented]:
The default can’t be a mutable object (model instance, list, set, etc.),
as a reference to the same instance of that object would be used as the
default value in all new model instances. Instead, wrap the desired
default in a callable.
In other words, you should use `default=dict`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28406#comment:1>
Comment (by Craig de Stigter):
I just got bit by this and it took several hours to track down what was
wrong. Some thoughts:
* Seems the docs should say "shouldn't" here rather than "can't", since
HStoreField happily accepts a mutable default value.
* There should be a load-time check for this. It's an easy mistake to
make, and very confusing / hard to track down.
* In fact, why doesn't the field reject `default={}` outright? Not every
mutable value is obvious, but dict instances are probably 99% of them, so
it could check for those.
--
Ticket URL: <https://code.djangoproject.com/ticket/28406#comment:2>
* cc: Craig de Stigter (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28406#comment:3>
Comment (by Tim Graham):
#28577 added checks for `JSONField` and `ArrayField` `default`. I added a
[https://github.com/django/django/pull/9852 PR] to do to the same for
`HStoreField`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28406#comment:4>