model_form

42 views
Skip to first unread message

Ruben.Moor

unread,
Apr 13, 2010, 4:07:15 PM4/13/10
to tipfy
Hello,
before I post my issues about model_form. Is it supposed to work
already?

Rodrigo Moraes

unread,
Apr 13, 2010, 6:42:59 PM4/13/10
to tipfy

Hi,
It is supposed to work. It needs testing though (I personally don't
use this kind of automated form). Please let me know if you have any
issues.

-- rodrigo

Rodrigo Moraes

unread,
Apr 13, 2010, 6:26:22 PM4/13/10
to tipfy
On Apr 13, 5:07 pm, "Ruben.Moor" wrote:

It is supposed to work. It needs a lot of testing, though (I

Ruben.Moor

unread,
Apr 14, 2010, 3:46:40 AM4/14/10
to tipfy
OK,

first issue:

In the request handler:

self.MemberForm = model_form(Member)
def get(self, **kwargs):
context = {}
context['form'] = self.MemberForm(obj=member, field_args={
'first_name': {
'label':'Vorname',
},
})

In the template:

<td>{{ form.first_name.label }}:<td>{{ form.first_name() }}

Yields as a result:
<td><label for="first_name">First Name</label>:<td><input
id="first_name" name="first_name" type="text" value="" />

Where the label should be "Vorname" instead of "First Name" according
to the documentation in the source code right?

Ruben.Moor

unread,
Apr 14, 2010, 3:52:10 AM4/14/10
to tipfy
second issue:

even though the generated code was

<td><label for="first_name">First Name</label>:<td><input
id="first_name" name="first_name" type="text" value="" />

where the input is definitely not a textarea, in my handler I have

form = self.MemberForm(request.form)
member.first_name = form.first_name

I am not sure about "requets.form" as argument for the constructor.
The error on post is:

** BadValueError: Property first_name must be a str or unicode
instance, not a TextField **

I have TextField in the form, but "first_name" is not.


One minor thing: Should "model_form" to be found in

from tipfy.ext.model.form import model_form

or

from wtforms.ext.appengine.db import model_form

Right now there is no model in tipfy.ext in the tarball.

Thanks a lot!

/ruben

Ruben.Moor

unread,
Apr 14, 2010, 4:03:23 AM4/14/10
to tipfy
Oh,

I realize, that I probably cannot rely on the latest tarball to get
access to a working model_form but I should rather checkout the source
and use

tipfy.ext.model.form:model_form right?

Sorry for that!
I guess this will solve my issues ...

Rodrigo Moraes

unread,
Apr 14, 2010, 7:10:17 AM4/14/10
to tipfy
On Apr 14, 5:03 am, "Ruben.Moor" wrote:
> I realize, that I probably cannot rely on the latest tarball to get
> access to a working model_form but I should rather checkout the source
> and use
>
> tipfy.ext.model.form:model_form right?
>
> Sorry for that!
> I guess this will solve my issues ...

Hey ruben,
The module is now part of wtforms (although initially it was part of
tipfy). The docblock was never updated to reflect this, now I
realized. So the import you should use is:

from wtforms.ext.appengine.db import model_form

-- rodrigo

Ruben.Moor

unread,
Apr 14, 2010, 3:09:15 PM4/14/10
to tipfy
In fact, if it is like that: I included the right file and the two
issues from above remain.

Rodrigo Moraes

unread,
Apr 18, 2010, 3:18:55 PM4/18/10
to tipfy
On Apr 14, 4:46 am, "Ruben.Moor" wrote:
> first issue:
>
> In the request handler:
>
>     self.MemberForm = model_form(Member)
>     def get(self, **kwargs):
>         context = {}
>         context['form'] = self.MemberForm(obj=member, field_args={
>             'first_name': {
>                 'label':'Vorname',
>                 },
>         })
>
> In the template:
>
> <td>{{ form.first_name.label }}:<td>{{ form.first_name() }}
>
> Yields as a result:
> <td><label for="first_name">First Name</label>:<td><input
> id="first_name" name="first_name" type="text" value="" />

Hey Ruben,
I'm looking back at these issues, sorry for the delay. I should have
spotted this at first when you posted. The example above is wrong. You
need to set field_args when you create the form, as in:

MemberForm = model_form(Member, field_args={
'first_name': {
'label':'Vorname',
},
})

... and not when you instantiate the form, later on. Hope this helps.

-- rodrigo

--
You received this message because you are subscribed to the Google Groups "tipfy" group.
To post to this group, send email to ti...@googlegroups.com.
To unsubscribe from this group, send email to tipfy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/tipfy?hl=en.

Rodrigo Moraes

unread,
Apr 18, 2010, 3:24:01 PM4/18/10
to tipfy
On Apr 14, 4:52 am, "Ruben.Moor" wrote:
> second issue:
>
> even though the generated code was
>
> <td><label for="first_name">First Name</label>:<td><input
> id="first_name" name="first_name" type="text" value="" />
>
> where the input is definitely not a textarea, in my handler I have
>
>     form = self.MemberForm(request.form)
>     member.first_name = form.first_name
>
> I am not sure about "requets.form" as argument for the constructor.
> The error on post is:
>
> ** BadValueError: Property first_name must be a str or unicode
> instance, not a TextField **

Hi again. :)
You pass request.form to the form constructor to populate it:
request.form is where POST variables are stored in the request object
(Werkzeug uses .form for POST and .args for GET variables).

In this case, to pass the values from the form to the entity, you need
to use the field data attribute, as in:

form = self.MemberForm(request.form)
if form.validate():
member.first_name = form.first_name.data

I also added validation just as an example. Hope it makes sense.

Ruben.Moor

unread,
Apr 20, 2010, 4:39:30 PM4/20/10
to tipfy
Thanks a lot.

How to get the byte content of a file field?

member.pic is a BlobProperty,
form.pic is a FileField

I want to upload images.


/ruben

Rodrigo Moraes

unread,
Apr 28, 2010, 10:11:14 AM4/28/10
to tipfy
On Apr 20, 5:39 pm, "Ruben.Moor" wrote:
> How to get the byte content of a file field?
>
> member.pic is a BlobProperty,
> form.pic is a FileField
>
> I want to upload images.

Hey,
If it still helps, you need to access the files attribute from request
to access uploaded files. It returns a FileStorage object that you can
read() the contents to store in a blog., So, for example, to access
the <input type="file" name="pic"> from the form:

pic = request.files.get('pic').read()

WTForms won't handle the file data because it is framework agnostic (I
think I should add this support). So it is accessed directly from the
request. Below is a full example:

class MyForm(Form):
pic = fields.FileField('pic')


class FormHandler(RequestHandler):
def get(self):
return render_response('form.html', form=MyForm())

def post(self):
form = MyForm(request.form)
pic = request.files.get('pic').read()
return Response(pic, mimetype='image/png')

And the basic template form.html:

<form method="post" enctype="multipart/form-data" action="">
{{ form.pic() }}
<input type="submit" name="submit" value="submit">
</form>
Reply all
Reply to author
Forward
0 new messages