Search code examples
djangomongodbdjango-modelsdjongo

Djongo redefine the structure of ArrayModelField


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,

Current Document Format

But I want to save it in this format,

Required Document 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.


Solution

  • 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.