Opened 6 years ago

Closed 6 years ago

#29388 closed Bug (invalid)

JSONField defaults to empty list gets modified

Reported by: Ponytech Owned by:
Component: contrib.postgres Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider this simple model :

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.

Change History (1)

comment:1 by Jani Tiainen, 6 years ago

Resolution: invalid
Status: newclosed

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

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