stephrdev/django-mongoforms — Issue #14: instance as EmbeddedDocument
Author: pomelo422
Created: 2012-01-31 08:23:24+00:00
State: open

Hi,

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 the 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 Author and Body of the comments:

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())

But apparently the post.comments is a BaseList not directly inherit from Document, so I got this error:

instance must be a mongoengine document, not BaseList

The question is what's the best practice in this case, say update the EmbeddedDocument?

Thank you!