librelist archives

« back to archive

How to assign a form field to a referenced field in Flask-WTF and Flask-MongoAlchemy?

How to assign a form field to a referenced field in Flask-WTF and Flask-MongoAlchemy?

From:
Miquel Joy
Date:
2012-10-07 @ 11:24
Hello,

I'm using Flask-MongoAlchemy and Flask-WTF (WTForms) to build a very basic
weblog. The usage of WTForms is based on the example in

https://github.com/cobrateam/flask-mongoalchemy/tree/master/examples/books_collection

As you can see in the listings below the class Post has a reference to
class Comment (using DocumentField). The problem I'm having is that I don't
know how to assign the content a "form field" to the "document field" in
such case (and viceversa), in particular comment.author and
comment.body. See the ??? in listing.

Has anyone any clue? Maybe this is a question more related to a MongoDB
forum, but as I'm using Flask extensions only I've addressed here.

(My apologies for my not accurate terminology)

In documents.py:

from collection import db
from flaskext.mongoalchemy import BaseQuery
 import re

class Comment(db.Document):
body = db.StringField(required=True)
 author = db.StringField(required=True)
 class Post(db.Document):
 title = db.StringField()
slug = db.StringField()
comment = db.DocumentField(Comment)                 # This is the field
with a reference to another document class

In forms.py:
 from flask.ext.wtf import Form, TextField, Required, IntegerField
 from collection.documents import Post, Comment

class PostForm(Form):
 document_class = Post
title = TextField(validators=[Required()])
slug = TextField(validators=[Required()])
 author = TextField(validators=[Required()])
body = TextField(validators=[Required()])
 instance = None

def __init__(self, document=None, *args, **kwargs):
 super(PostForm, self).__init__(*args, **kwargs)
if document is not None:
 self.instance = document
self._copy_data_to_form()

def _copy_data_to_form(self):
 self.title.data = self.instance.title
self.slug.data = self.instance.slug
 self.author.data = self.instance.comment???author???       # These are the
problematic assignments
self.body.data = self.instance.comment???body??? #
 def save(self):
if self.instance is None:
 self.instance = self.document_class()
self.instance.title = self.title.data
 self.instance.slug = self.slug.data
self.instance.comment???author??? = self.author.data # These are the
problematic assignments
 self.instance.comment???body??? = self.body.data #
self.instance.save()
 return self.instance
Thanks in advance.

Re: [flask] How to assign a form field to a referenced field in Flask-WTF and Flask-MongoAlchemy?

From:
Andres Rand
Date:
2012-10-08 @ 13:48
Hello!

In _copy_data_to_form() you can use self.instance.comment.author and
self.instance.comment.body
In save() you can do:
<snip>
self.instance.slug = self.slug.data
comment = Comment(body = self.body.data, author = self.author.data)
slef.instance.comment = comment
comment.save()
self.instance.save()
<snip>

First you must create new comment object and then add it to self.instance.
Note that you must save both instance and comment!
If you do self.instance.comment.body and self.instance.comment.author
then mongo creates
sub document but does not assign ObjectID to it. Likewise, if you do
not save comment then
you can still access it form Post.comment.body/author but not by
separate object - Comment.find() does show them.

Hth,

--
Andres Rand
hrrand@gmail.com

Re: [flask] How to assign a form field to a referenced field in Flask-WTF and Flask-MongoAlchemy?

From:
Miquel Joy
Date:
2012-10-08 @ 16:25
Hello Andres,

Excellent! It works! It's plenty of logic what you say. Also thanks for
emphasizing the difference between embedded and reference documents to a
MongoDB newbie like I am.

Thanks a lot!

Miquel