问题描述
我正在学习Django,并因在模型中创建json字段而感到沮丧。我试图在模型中创建json字段,但出现错误:模块对象没有属性 JSONField。这是我在models.py中的课程:
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我正在使用Django 1.9.8和PostgreSQL 9.2.13。
我需要在postgresql db中创建的表具有JSON类型的列。我该如何在模型课程中做到这一点?
谢谢!
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!
推荐答案
模型中没有 JSONField
。但是有一个方便的 jsonfield
包可用于在Django模型中使用 JSONField
。要安装该软件包,请执行以下操作:
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
一旦安装,执行:
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')
这篇关于Django JSON字段。 “模块”对象没有属性“ JSONField”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!