{{{
from django.db import models
from django.contrib.postgres.fields import JSONField
class Thing(models.Model):
name = models.CharField(max_length=100)
features = JSONField(default=[], blank=True)
}}}
And this code snippet :
{{{
>>> t1 = Thing.objects.create(name="thing 1")
>>> t1.features.append({"f1": 42, "f2": 56})
>>> t1.features
[{'f1': 42, 'f2': 56}]
>>> t1.save()
>>> t2 = Thing.objects.create(name="thing 2")
>>> t2.features
[{'f1': 42, 'f2': 56}]
}}}
I'd expect t2 features to be an empty list.
My guess is the model default fields gets modified somehow.
--
Ticket URL: <https://code.djangoproject.com/ticket/29388>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
You've hit Python mutable default problem. Instead of defining mutable
default `[]` use callable as default `list` in this particular case.
See [[TicketClosingReasons/UseSupportChannels]] for further information
--
Ticket URL: <https://code.djangoproject.com/ticket/29388#comment:1>