Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django JSON field. 'module' object has no attribute 'JSONField'

Tags:

python

django

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!

like image 717
Gary Avatar asked Jul 21 '16 20:07

Gary


2 Answers

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.

like image 150
Shang Wang Avatar answered Oct 21 '22 05:10

Shang Wang


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

like image 38
Cesar Canassa Avatar answered Oct 21 '22 06:10

Cesar Canassa