Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling MongoEngine's DynamicEmbeddedDocument in Flask-Admin

I have a problem for which I can't find a simple solution, using Flask-Admin with MongoEngine. I have a Document class named ExerciseResourceContent. It has a "questions" attribute, which is a ListField of an EmbeddedDocument called ExerciseQuestion:

class ExerciseResourceContent(ResourceContent):
    """An exercise with a list of questions."""

    ## Embedded list of questions
    questions = db.ListField(db.EmbeddedDocumentField(ExerciseQuestion))

The ExerciseQuestion document is actually a DynamicEmbeddedDocument:

class ExerciseQuestion(db.DynamicEmbeddedDocument):
    """
    Generic collection, every question type will inherit from this.
    Subclasses should override method "without_correct_answer" in order to define the version sent to clients.
    Subclasses of questions depending on presentation parameters should also override method "with_computed_correct_answer".
    """

    _id = db.ObjectIdField(default=ObjectId)

    ## Question text
    question_text = db.StringField(required=True)

    ## Correct answer (field type depends on question type)
    correct_answer = db.DynamicField()

It can be subclassed in two classes (more to come): MultipleAnswerMCQExerciseQuestion and UniqueAnswerMCQExerciseQuestion:

class MultipleAnswerMCQExerciseQuestion(ExerciseQuestion):
    """Multiple choice question with several possible answers."""

    ## Propositions
    propositions = db.ListField(db.EmbeddedDocumentField(MultipleAnswerMCQExerciseQuestionProposition))

    ## Correct answer
    correct_answer = db.ListField(db.ObjectIdField())

class UniqueAnswerMCQExerciseQuestion(ExerciseQuestion):
    """Multiple choice question with one possible answer only."""

    ## Propositions
    propositions = db.ListField(db.EmbeddedDocumentField(UniqueAnswerMCQExerciseQuestionProposition))

    ## Correct answer
    correct_answer = db.ObjectIdField()

When I use Flask-Admin to create or edit an ExerciseResourceContent, it displays a "Question" list, from which I can edit a "Question_text" attribute, but I can't see "Correct_Answer" attribute, nor any "Propositions" attribute as I would. I struggled with the Flask-Admin doc, but it seems that's a problem with Dynamic stuff (fields or documents), and there's nothing about it in the docs.

Thanks for your help

like image 816
Frédéric Fourcade Avatar asked Apr 03 '15 17:04

Frédéric Fourcade


1 Answers

It seems to me that you have to customise the admin-view for your models. It is a task that you have to do for your models if they are not displayed correctly "out of the box".

In most of the cases you don't have to fully rewrite the views. In most of the cases it will be enough to customise built-in views.

I don't have any experience developing for flask, but you will basically have to subclass ModelView. and register the subclass to the admin

#Customized admin views
class ExerciseQuestionView(ModelView):
 #
 # add customisation code here
 #

class MultipleAnswerMCQExerciseQuestionView(ModelView):
 #
 # add customisation code here
 #

class UniqueAnswerMCQExerciseQuestionView(ModelView):
 #
 # add customisation code here
 #

if __name__ == '__main__':
    # Create admin
    admin = admin.Admin(app, 'Example: MongoEngine')

    # Add admin views
    admin.add_view(ExerciseQuestionView(ExerciseQuestion))
    admin.add_view(MultipleAnswerMCQExerciseQuestionView(MultipleAnswerMCQExerciseQuestion))
    admin.add_view(UniqueAnswerMCQExerciseQuestionView(UniqueAnswerMCQExerciseQuestion))
    #...

Anyway i think you should go through the documentation... or you may end up waiting too long here...

http://flask-admin.readthedocs.io/en/latest/api/mod_contrib_mongoengine/

like image 129
Ouss Avatar answered Sep 20 '22 16:09

Ouss