﻿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
37304	Incorrect migration generated when a JSONField have a local scope defined Decoder	takuyozora	Yassin Bahri	"Hi,

I have a JSONField on one of my model with a custom JSONDecoder : 

{{{#!python
class UnitAbstract(models.Model):
    class Meta:
        abstract = True
    # ...
    base_stats = models.JSONField(default=rpg.stats.RPGBaseStats(
        pv={'maximum': 100},
        mana={'maximum': 0}
    ).toJSON(), encoder=RPGJSONEncoder, decoder=rpg.stats.RPGBaseStats.getJSONDecoder())

class Unit(UnitAbstract):
    pass
}}}

But the migration file generated by `makemigration` is incorrect (because of this part : `decoder=terrarpg.rpg.mixins.DeconstructibleMixin.getJSONDecoder.<locals>.RPGJSONDecoder`) :


{{{#!python
class Migration(migrations.Migration):

    dependencies = [
        ('terrarpg', '0035_unitgroup_formation_rank_alter_unit_base_stats_and_more'),
    ]

    operations = [
        migrations.AlterField(
            model_name='unit',
            name='base_stats',
            field=models.JSONField(decoder=terrarpg.rpg.mixins.DeconstructibleMixin.getJSONDecoder.<locals>.RPGJSONDecoder, default=[
#...
], encoder=terrarpg.rpg.parsers.RPGJSONEncoder),
        ),
}}}

I assume the '''<locals>''' that produce the syntax error is here because my decoder is a dynamically generated class as you can see here : 

{{{#!python
class DeconstructibleMixin(abc.ABC):
    # ...
    @classmethod
    def getJSONDecoder(cls):
        class RPGJSONDecoder(json.JSONDecoder):
            def decode(self, *args, **kwargs) -> cls:
                data = json.JSONDecoder.decode(self, *args, **kwargs)
                if isinstance(data, list) and len(data) >= 1:
                    return cls.fromJSON(data)
                return data
        return RPGJSONDecoder
}}}

If I do not apply the migration everything is working properly (because the field have been created before without the custom decoder). 
So maybe either Django should not produce a migration file when only the decoder part change on a JSONField because it's not link to the DB schema, or it should handle properly the fact that the decoder is a local scope defined class.

I hope that the bug issue is filled properly and that my English is compressible enough."	Bug	assigned	Migrations	6.1	Normal		JSONField migration	takuyozora	Accepted	1	0	0	0	0	0
