I have a model with a PostGRES JSONField:
from django.contrib.postgres.fields import JSONField
# ... other imports ...
class Feature(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
# ... other fields ...
meta = JSONField(default=dict)
And an importer command that either creates or updates features:
my_meta = {
'mykey': 'something',
}
feature = Feature.objects.filter(id=id).first()
if feature is None:
# The feature was not imported previously
feature = Feature.objects.create(
id=id,
meta=my_meta,
)
print('CREATED FEATURE.META', feature.meta, feature.meta.__class__.__name__)
else:
# The feature was already imported above - update the existing feature with new metadata
feature.meta = my_meta,
feature.save()
print('UPDATED FEATURE.META', feature.meta, feature.meta.__class__.__name__)
When running two different test cases, each creating one feature but testing the two branches of that 'if' statement, I get:
CREATED FEATURE.META {'mykey': 'something'} dict
UPDATED FEATURE.META ({'mykey': 'something'},) tuple
THE QUESTION
Why on earth is it decoding inside a tuple in that latter case?
NOTES
Yes, my default is a callable (common issue ppl have with JSONField)
No, I don't have
django-jsonfieldinstalled (which can cause weird incompatibilities with the native JSONField)