[Fixed]-Django JSON field. 'module' object has no attribute 'JSONField'

15👍

There’s no JSONField in models. But there’s a handy jsonfield package available to use JSONField in Django models. To install the package, do:

pip install jsonfield

Once installed, do:

from jsonfield import JSONField
from django.db import models

class Question(models.Model):
question_text = JSONField(max_length=200)
pub_date = models.DateTimeField('date published')

25👍

There’s no JSONField in models module, you need to:

from django.contrib.postgres.fields import JSONField

class Question(models.Model):
    question_text = JSONField()

Django doc about JSONField.

19👍

Django 3.1 update

Starting with Django 3.1, the JSONField is now available for all database backends.

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.JSONField

1👍

Changing the import statement should fix your problem (it helped me):

from django.contrib.postgres.fields.jsonb import JSONField

So JSONField is located in jsonb module now.

Leave a comment