Not a valid choice: Can't select ReferenceProperty value at SelectField wtform

713 views
Skip to first unread message

sergey

unread,
Jun 21, 2011, 9:34:20 AM6/21/11
to tipfy
i try to use ReferenceProperty at form for create/edit Entry but
nothing happen.

i have:
class Type(db.Model):
name = db.StringProperty()

class Entry(db.Model):
type = db.ReferenceProperty(Type, required=False)

class EntryForm(Form):
_type_list = []
for type in Type.all():
_type_list.append((type.key(),type.name))
type = fields.SelectField(u'Type of entry', choices =
_type_list)

and edit handler:

def post(self, **kwargs):
self.form = EntryForm(self.request.form)
if self.form.validate():
values = {
'type':
models.Type.get_by_key_name(self.form.type.data).key(),
}
entry = Entry(**values)
entry.put()

but i always have: Not a valid choice

Does enyone know how to work with ReferenceProperty in wtforms
SelectField or may you have working sample for this?

sergey

unread,
Jun 22, 2011, 2:13:28 AM6/22/11
to tipfy
I solve my problem with this changes at Form class:

class EntryForm(Form):
_type_list = []
for type in Type.all():
_type_list.append((type.key().id(),type.name))
type = fields.SelectField(u'Type of entry', choices =
_type_list, coerce=int)
and edit handler:

def post(self, **kwargs):
self.form = EntryForm(self.request.form)
if self.form.validate():
values = {
'type': models.Type.get_by_id(self.form.type.data),
}
entry = Entry(**values)
entry.put()
But if anyone know more elegant solution, you are welcome!
Reply all
Reply to author
Forward
0 new messages