我有一个问题,我找不到一个简单的解决方案,使用Flask-Admin和MongoEngine。我有一个名为ExerciseResourceContent
的文档类。它有一个“ListField
”属性,这是一个名为ExerciseQuestion
的EmbeddedDocument
的问题
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文档,但这似乎是动态内容(字段或文档)的问题,并且文档中没有任何相关内容。
谢谢你的帮忙
发布于 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/
发布于 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)
https://stackoverflow.com/questions/29436798
复制相似问题