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:
JavaScript
x
4
1
class Question(models.Model):
2
question_text = models.JSONField(max_length=200)
3
pub_date = models.DateTimeField('date published')
4
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:
JavaScript
1
2
1
pip install jsonfield
2
Once installed, do:
JavaScript
1
7
1
from jsonfield import JSONField
2
from django.db import models
3
4
class Question(models.Model):
5
question_text = JSONField(max_length=200)
6
pub_date = models.DateTimeField('date published')
7