Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:21:26+00:00 2026-05-28T19:21:26+00:00

Here is a simple scenario: I have a blog post with comments attach to

  • 0

Here is a simple scenario: I have a blog post with comments attach to it. Now I want to use mongoforms to let user submit their comments. Therefore I have this models.py:

class Post(Document):
    title = StringField(max_length=60)
    body = StringField()
    created = DateTimeField(default=datetime.datetime.now)
    comments = ListField(EmbeddedDocumentField('Comment'))

class Comment(EmbeddedDocument):
    author = StringField(max_length=60)
    body = StringField()
    created = DateTimeField(default=datetime.datetime.now)

And I’m using django-mongoforms to support the similar ModelForm functionality, within forms.py:

class CommentForm(MongoForm):
    class Meta:
        document = Comment
        fields = ('author', 'body')

In the views.py, I just want to save what user has entered, more specifically the Author and Body fields of a comment, here is the method to add a comment:

def add_comment(request, id):
    post = Post.objects.with_id(id)
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=post.comments)
        if form.is_valid():
            form.save()
    return HttpResponseRedirect(post.get_absolute_url())

Then once I click Submit button here comes the error message:

instance must be a mongoengine document, not BaseList

So questions are:

  1. How to covert BaseList to Document in MongoEngine? I have looked through the document without a hint.
  2. What’s the best practices here generally to handle the EmbeddedDocument? Will ReferenceDocument help?

Also there is a related questions here but without an answer:
What is the proper way to update a listfield of embedded documents in mongoengine?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:21:27+00:00Added an answer on May 28, 2026 at 7:21 pm

    As comments are embedded you’ll need to find and update the existing comment in the post object. You can then overwrite the old comment with the new comment (where i is the index) eg:

    post.comments[i] = new_comment
    

    then just do a post.save() and mongoengine will convert that to a $set operation.

    Alternatively, you could just write the $set directly eg:

    Post.objects(pk=post.pk).update(set__comments__i=comment)
    

    Forms.py

    I still use the forms.py but I would pass commit=False to save that will return the comment – then append to the post and save the post like so:

    def add_comment(request, id):
        post = Post.objects.with_id(id)
        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                comment = form.save(commit=False)
                post.comments.append(comment)
                post.save()
        return HttpResponseRedirect(post.get_absolute_url())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my scenario: I have a simple stored procedure that removes a specific set
Here is a simple scenario: assuming I have two Core Data entities: A and
I have been trying to do a very simple auditing scenario following Ayende's blog
Here is the simple scenario that I want to apply; There is a .NET
my question is very simple. here is my scenario: i have 1 result dataset[named=resultDS
we have a (in our oppinion) very simple scenario here. But we got stuck
Here is a simple scenario with table characters: CharacterName GameTime Gold Live Foo 10
Please excuse me if I'm simple here, I want to create a simple widget
I hope I am not missing something very simple here. I have done a
Here's a simple (hopefully) L10N question: Do all locales want this format: Sunday, Nov

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.