首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Flask管理中处理MongoEngine的DynamicEmbeddedDocument

在Flask管理中处理MongoEngine的DynamicEmbeddedDocument
EN

Stack Overflow用户
提问于 2015-04-04 01:19:06
回答 2查看 1.2K关注 0票数 16

我有一个问题,我找不到一个简单的解决方案,使用Flask-Admin和MongoEngine。我有一个名为ExerciseResourceContent的文档类。它有一个“ListField”属性,这是一个名为ExerciseQuestionEmbeddedDocument的问题

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文档,但这似乎是动态内容(字段或文档)的问题,并且文档中没有任何相关内容。

谢谢你的帮忙

EN

回答 2

Stack Overflow用户

发布于 2017-05-23 16:33:01

在我看来,你必须为你的模型定制管理视图。如果模型“开箱即用”不能正确显示,这是您必须为模型执行的任务。

在大多数情况下,您不必完全重写视图。在大多数情况下,定制内置视图就足够了。

我没有任何为flask开发的经验,但基本上你必须将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/

票数 1
EN

Stack Overflow用户

发布于 2017-03-06 20:12:38

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) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29436798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档