在settings.py中将DJONGO用作引擎时如何在django models.py中使用ListField

时间:2019-06-13 12:32:40

标签: django mongodb django-2.2 djongo

我的json数据看起来像这样

{
    "Batch Year": 1998 - 1999,
    "Roll Nos": ["36", "125"],
    "Student Names": ["xyz", "xxy"],
    "Project Name": "IMPLEMENTATIONOF NETWORK SECURITY BY VIRTUAL PRIVATE NETWORK (VPN)",
    "Project Supervisor": "abc",
    "External Supervisor": "aaa",
    "Co-Supervisor": "None",
    "Project Id": 155
}

对于此数据,我在models.py中为编号和学生姓名定义了ListFields,但是当我使用管理面板在数据库中添加数据时,我遇到了错误:

  

”异常值:   无法从Django Admin修改无格式字段。”

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'fypcis_db',
    }

models.py

from __future__ import unicode_literals
from djongo import models
import djongo

class students(models.Model):
    batch_year = models.IntegerField(db_column='Batch Year')  # Field name made lowercase.
    roll_nos = djongo.models.ListField(db_column='Roll Nos',default=True)  # Field name made lowercase.
    student_name = djongo.models.ListField(db_column='Student Names',default=True)  # Field name made lowercase.
    project_name = models.CharField(max_length=250, db_column='Project Name',default=True)  # Field name made lowercase.
    project_supervisor = models.CharField(max_length=250, db_column='Project Supervisor',default=True)  # Field name made lowercase.
    external_supervisor = models.CharField(max_length=250,db_column='External Supervisor',default=True)  # Field name made lowercase.
    co_supervisor = models.CharField(max_length=250,db_column='Co-Supervisor',default=True)  # Field name made lowercase.
    project_id = models.IntegerField(db_column='Project Id')  # Field name made lowercase.

请帮助我解决此错误。 预先感谢

我正在使用django 2.2和djongo作为引擎

1 个答案:

答案 0 :(得分:0)

https://nesdis.github.io/djongo/using-django-with-objectid-field/ ArrayModelField和ArrayReferenceField要求列表中的所有Model均为同一类型。 MongoDB允许在嵌入式数组内部保存任意数据。 ListField在这种情况下很有用。列表字段不能在Django Admin中表示,只能在python脚本中使用。