2015-04-03 58 views
16

我有一個問題,我找不到一個簡單的解決方案,使用Flask-Admin和MongoEngine。 我有一個名爲ExerciseResourceContent的文檔類。它有一個「問題」屬性,這是一個EmbeddedDocumentListField稱爲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(字段或文檔)的問題,並且文檔中沒有任何關於它的信息。

感謝您的幫助

回答

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) 
+1

雖然此代碼回答這個問題,提供關於這個問題爲什麼和/或如何回答這個問題的附加背景來提高它的長期價值。 – 2017-03-10 12:25:55

1

在我看來,你必須自定義管理視圖爲您的模型。如果模型顯示不正確,那麼您需要爲模型執行一項任務。

在大多數情況下,您不必完全重寫視圖。在大多數情況下,它將足以定製內置視圖。

我沒有任何開發燒瓶的經驗,但基本上你必須繼承ModelView。並註冊子類來管理

#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)) 
    #... 

無論如何,我認爲你應該去通過文檔...或者你可能最終等待太久這裏...

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