基于MongoEngine创建反馈表单动态模型的技术求助
Fixing Your MongoEngine Models for the Feedback Form Structure
Hey there! Let's get your MongoEngine models sorted to perfectly match the feedback form document structure you shared. I'll walk through this step by step, starting with breaking down your target structure and then building the right models.
First, let's recap the target document structure you're aiming for (formatted for clarity):
[ { "Question1": "How do you rate the whole restaurant", "Rating": ["Poor", "Average", "Good"], "next": [ { "Question2": "How do you rate food plates cleanness", "Rating": ["Poor", "Average", "Good"] }, { "Question3": "How do you rate floor cleanness", "Rating": ["Poor", "Average", "Good"] } ] }, { "Question4": "How do you rate food taste", "Rating": ["Poor", "Average", "Good"] } ]
Key Observations About the Structure
- Each entry (top-level or nested in
next) is a question with:- A question text (like "How do you rate the whole restaurant")
- A list of rating options (Poor/Average/Good)
- Optional nested follow-up questions (the
nextarray)
- Using dynamic keys like
Question1,Question2isn't ideal for MongoEngine—we'll use consistent field names instead, which makes the model cleaner and easier to query.
Updated MongoEngine Models
Here's the revised model structure that maps perfectly to your needs:
from mongoengine import Document, EmbeddedDocument, StringField, ListField, EmbeddedDocumentField class Question(EmbeddedDocument): # Stores the actual question text (e.g., "How do you rate the whole restaurant") question_text = StringField(required=True) # Stores the available rating options rating_options = ListField(StringField(), required=True) # Stores nested follow-up questions (recursive embedded document) next_questions = ListField(EmbeddedDocumentField('Question'), default=list) class FeedbackFormTemplate(Document): # The top-level list of questions for the form questions = ListField(EmbeddedDocumentField(Question), required=True) # Optional: Add a name or identifier for the template if you need multiple forms template_name = StringField(unique=True)
Why This Works
- Recursive
next_questions: TheQuestionclass references itself innext_questions, which lets you nest follow-up questions as deep as needed—exactly like yournextarray in the target structure. - Consistent field names: Instead of dynamic
QuestionXkeys, we usequestion_textto store the question content. This is far more maintainable than relying on numbered keys. - Simplified rating storage: We replaced the separate
Ratingsembedded document with a directListField(StringField())forrating_options—since yourRatingarray is just a list of strings, this is more straightforward (you can always expand it into an embedded document later if you need to add metadata to ratings).
Example: Creating the Feedback Form Template
Here's how you'd create an instance that matches your target structure:
# Create nested follow-up questions plate_cleanliness_q = Question( question_text="How do you rate food plates cleanness", rating_options=["Poor", "Average", "Good"] ) floor_cleanliness_q = Question( question_text="How do you rate floor cleanness", rating_options=["Poor", "Average", "Good"] ) # Create top-level questions restaurant_rating_q = Question( question_text="How do you rate the whole restaurant", rating_options=["Poor", "Average", "Good"], next_questions=[plate_cleanliness_q, floor_cleanliness_q] ) food_taste_q = Question( question_text="How do you rate food taste", rating_options=["Poor", "Average", "Good"] ) # Create and save the template form_template = FeedbackFormTemplate( template_name="Restaurant Feedback Form", questions=[restaurant_rating_q, food_taste_q] ) form_template.save()
When you query this document from MongoDB, it will map closely to your original structure—just with more consistent field names that are easier to work with in code.
内容的提问来源于stack exchange,提问作者user3982364
相关产品推荐
相关产品推荐

