Search code examples
djangomongodbdjango-2.2djongo

How to use ListField in django models.py while using DJONGO as engine in settings.py


my json data seems like this

{
    "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
}

for this data i defined ListFields in models.py for Roll Nos and Student Names but when i was using admin panel to add data im my database i am facing error:

"Exception Value: A Formless Field cannot be modified from 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.

please help me to solve this error. Thanks in advance

i am using django 2.2 and djongo as Engine


Solution

  • https://nesdis.github.io/djongo/using-django-with-objectid-field/ ArrayModelField and ArrayReferenceField require all Models in the list to be of the same type. MongoDB allows the saving of arbitrary data inside it is embedded array. The ListField is useful in such cases. The list field cannot be represented in Django Admin though and can only be used in the python script.