Home > Back-end >  Django: Updating Old Migration Files that Have `django_mysql.models.JSONField`?
Django: Updating Old Migration Files that Have `django_mysql.models.JSONField`?

Time:06-29

In older versions of Django, you could use JSON fields in models via django_mysql.models.JSONField. In new versions of Django, JSONField is no longer in django_mysql.models. I've updated my models.py files accordingly, but I still have old migrations files that look like this:

# Generated by Django 2.1.7 on 2019-07-17 22:59

from django.db import migrations
import django_mysql.models


class Migration(migrations.Migration):

    dependencies = [
        ('rss', '0009_delete_patternmatchingkeywords'),
    ]

    operations = [
        migrations.AddField(
            model_name='rssoutput',
            name='industries',
            field=django_mysql.models.JSONField(default=list), ##<== ERROR
        ),
    ]

Now when I run makeMigration, I get this error:

AttributeError: module 'django_mysql.models' has no attribute 'JSONField'

What is the correct procedure to address this?

I'm using:

  • Django 4.0
  • Python 3.9.13
  • django-mysql 4.7.0

CodePudding user response:

Simply change it to proper JSONField that Django suggests:

from django.db.models import JSONField

...

class Migration(migrations.Migration):
    ...    
    operations = [
        migrations.AddField(
            ...
            field=JSONField(default=list)
        ),
    ]
  • Related