﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31930	Using a builtin callable as a default arg for JSONField.	Stuart Buckingham	nobody	"I have a JSONField that I need to apply a default dictionary to. As per the documentation, I am avoiding passing the mutable dictionary to the default field. This is done by instead passing the copy method to the default argument like such:

{{{
default_dict = {'some_key': 'some value'}

class MyModel(models.Model):
    my_field = models.JSONField(default=default_dict.copy)
}}}

When applying makemigrations, this is failing because of the following condition in django.db.migrations.serializer.FunctionTypeSerializer:
{{{
if self.value.__module__ is None:
    raise ValueError(""Cannot serialize function %r: No module"" % self.value)
}}}

I can get around this by defining a callable that returns a copy, but I think this is adding unnecessary syntax and makes it harder to read:
{{{
class ADict(dict):
    def __call__(self):
        return self.copy()

default_dict = ADict({'some_key': 'some value'})

class MyModel(models.Model):
    my_field = models.JSONField(default=default_dict)
}}}"	New feature	closed	Migrations	3.1	Normal	wontfix	Migrations default field callable		Unreviewed	0	0	0	0	0	0
