Skip to content
Advertisement

Django JSON field. ‘module’ object has no attribute ‘JSONField’

I am learning Django and was frustrated by creating a json field in a model. I was trying to create a json field in my model but got error: ‘module’ object has no attribute ‘JSONField’. Here is my class in models.py:

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

I am using django 1.9.8 and postgresql 9.2.13. I need the table created in postgresql db has a column with JSON type. How can I do that in the model class? Thank you!

Advertisement

Answer

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')
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement