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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With