Reputation: 2139
I am defining ArrayModelField using Djongo. It looks great, but the problem with ArrayModelField
is I can only add it as Array of Objects, not just Flat List. Is there any way by which I can add it as Flat List?
Example:
# models.py
from djongo import models
class Option(models.Model):
option = models.CharField(max_length=100)
class Meta:
abstract = True
def __str__(self):
return self.option
class Quiz(models.Model):
_id = models.ObjectIdField()
question = models.TextField(null=True, blank=True)
options = models.ArrayModelField(model_container=Option)
answers = models.ArrayModelField(model_container=Option)
Using this I can create a document as follows,
But I want to save it in this format,
I want the view to be parsed in Django Admin Panel too.
Is there any way by which I can do this now?
Thanks in advance.
Upvotes: 3
Views: 1605
Reputation: 1
For your use case, where the database connected is a MongoDB, you can do this:
from djongo import models
from django.contrib.postgres.fields import ArrayField
class Option(models.Model):
.
.
.
class Quiz(models.Model):
_id = models.ObjectIdField()
question = models.TextField(null=True, blank=True)
options = models.ArrayField(models.CharField(max_length=1024, blank=True),size=8)
this will show your options in a comma-separated manner i.e. A,B,C attaching a screenshot for example
Upvotes: 0
Reputation: 36
As of latest version in Djongo, this structure is still not possible if you want to have automatic support for Admin Panel. Having said that, you can always use ListField
and write custom view for the admin panel which is a valid solution for now. Also don't see any timeline in which that will be fixed by the developer.
Upvotes: 2