I am currently on Django 1.7 and my database is PostgreSQL. I have the following model.
# myapp/models.py
from django.db import models
from json_field import JSONField # using django-json-field
from json_field.fields import JSONDecoder
class MyModel(models.Model):
the_data = JSONField(
decoder_kwargs={'cls': JSONDecoder, 'parse_float': float},
default='[{}]',
)
...
I am now looking to upgrade to Django 1.10 and take advantage of the new(ish) JSONField
in django.contrib.postgres.fields
.
I change my model to look like this.
# myapp/models.py
from django.db import models
from django.contrib.postgres.fields import JSONField # Now want to use the new JSONField
class MyModel(models.Model):
the_data = JSONField(
default='{}',
)
...
I then create a migration for the app.
./manage.py makemigrations myapp
When it attempts to create a migration it complains...
from json_field.forms import JSONFormField
File "/Users/travismillward/Projects/amcards_env/lib/python2.7/site-packages/json_field/forms.py", line 5, in <module>
from django.forms import fields, util
ImportError: cannot import name util
I understand why it is complaining. django-json-field is not updated for django 1.10 and it wants to import json_field
in one of the original migration files. So I can either go back and modify my original migration file that imports json_field but then it won't actually modify the column data type because it thinks it is already done. Or, I have to fix django-json-fields to work with django 1.10 just so the migration can be created. And I will have to leave that requirement in place even though I don't use it, it's just for the migration!
On my last project I just modified the original migration to make it think that it was using django.contrib.postgres.fields.jsonb.JSONField
all along. However, after I ran the migration, it didn't change the column's data type from text
to jsonb
. So I manually did that since it was a smaller project. For this project, I really don't want to manually alter the database.
Any suggestions on how to migrate away from django-json-field gracefully and with a plan to remove it from my code and requirements?