我有一個問題,我找不到一個簡單的解決方案,使用Flask-Admin和MongoEngine。 我有一個名爲ExerciseResourceContent
的文檔類。它有一個「問題」屬性,這是一個EmbeddedDocument
的ListField
稱爲ExerciseQuestion
:在Flask-Admin中處理MongoEngine的DynamicEmbeddedDocument
class ExerciseResourceContent(ResourceContent):
"""An exercise with a list of questions."""
## Embedded list of questions
questions = db.ListField(db.EmbeddedDocumentField(ExerciseQuestion))
的ExerciseQuestion
文件實際上是一個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()
它可以在兩個類被繼承(更來):MultipleAnswerMCQExerciseQuestion和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()
當我使用Flask-Admin創建或編輯ExerciseResourceContent
時,它會顯示一個「問題」列表,我可以從中編輯「Question_text」屬性,但我看不到「Correct_Answer」屬性或任何「Propositions」屬性如我所願。 我用Flask-Admin文檔掙扎着,但它似乎是Dynamic stuff(字段或文檔)的問題,並且文檔中沒有任何關於它的信息。
感謝您的幫助
雖然此代碼回答這個問題,提供關於這個問題爲什麼和/或如何回答這個問題的附加背景來提高它的長期價值。 – 2017-03-10 12:25:55