Handling MongoEngine's DynamicEmbeddedDocument in Flask-Admin
Asked Answered
L

2

16

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

Levitate answered 3/4, 2015 at 17:19 Comment(0)
L
1

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/

Lemmuela answered 23/5, 2017 at 8:33 Comment(0)
H
0
import time

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split() 
another = [0]
time.sleep(0.5)
print(sentence)
    for count, i in enumerate(s): 
    if s.count(i) < 2:
        another.append(max(another) + 1)
    else:
        another.append(s.index(i) +1)
another.remove(0)
time.sleep(0.5)
print(another)
file = open("N:\(Filedirectory)","w")
file.write(another)  
file.write(s) 
Honourable answered 6/3, 2017 at 12:12 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Salad

© 2022 - 2024 — McMap. All rights reserved.