Created
October 20, 2012 09:23
-
-
Save hetsch/3922752 to your computer and use it in GitHub Desktop.
WTForms populate_obj problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wtforms.form import Form | |
from wtforms.fields import TextField, TextAreaField, FieldFist, FormField | |
class PluginForm(Form): | |
tags = TextField('Some random tags',) | |
files = TextField('Some random files',) | |
class Plugin(object): | |
form = PluginForm | |
class Page(object): | |
@classmethod | |
def register_plugin(cls, plugin): | |
if issubclass(plugin, Plugin) and plugin not in cls._plugins: | |
cls._plugins.append(plugin) | |
class PageForm(Form): | |
title = TextField('Title of the Page',) | |
body = TextAreaField('Enter the text for this page') | |
... | |
plugins = FieldList(FormField(PluginForm)) | |
def __init__(self, *args, **kwargs): | |
super(PageForm, self).__init__(*args, **kwargs) | |
for plugin in Page._plugins: | |
plugin_form_cls = plugin.form | |
self.plugins.append_entry(FormField(plugin_form_cls)) | |
Page.register_plugin(Plugin) | |
class AttributeDict(dict): | |
__getattr__ = dict.__getitem__ | |
__setattr__ = dict.__setitem__ | |
def create_post(request): | |
post = AttributeDict() | |
form = PageForm(request.POST, obj=post) | |
""" | |
form.data contains following values: | |
{ | |
'body': u'', | |
'meta_description': u'', | |
'meta_keywords': u'', | |
'title': u'seas', | |
'published': True, | |
'meta_title': u'', | |
'publication_end_date': datetime.datetime(2012, 11, 19, 11, 43, 44), | |
'plugins': [ | |
{'files': u'xxx', 'tags': u'xxx'}, | |
{'files': u'yyy', 'tags': u'yyy'}, | |
], | |
'publication_date': datetime.datetime(2012, 10, 19, 11, 43, 44), | |
'in_navigation': False | |
} | |
""" | |
if request.POST and form.validate(): | |
form.populate_obj(post) | |
return redirect('/home') | |
return render_to_response('edit_post.html', form=form) |
Thanks! Your example saved me! I've used it to create a self contained example: https://github.com/sebkouba/dynamic-flask-form
Thank you for this gist.Check this I created this inspired from your gist.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create_post throws exception
TypeError: populate_obj: cannot find a value to populate from the provided obj or input data/defaults
with following stacktrace:
Traceback (most recent call last):
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/app.py", line 1701, in call
return self.wsgi_app(environ, start_response)
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functionsrule.endpoint
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/views.py", line 83, in view
return self.dispatch_request(_args, *_kwargs)
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/flask/views.py", line 150, in dispatch_request
return meth(_args, *_kwargs)
File "/Users/xxx/Documents/Projekte/aaa/helpers.py", line 110, in decorated_function
ctx = f(_args, *_kwargs)
File "/Users/xxx/Documents/Projekte/aaa/blueprints/page/admin/views.py", line 30, in post
return super(CreateView, self).post(language)
File "/Users/xxx/Documents/Projekte/aaa/blueprints/admin/core/views.py", line 68, in post
self.form.populate_obj(entity)
File "/Users/xxx/Documents/Projekte/aaa/blueprints/page/admin/forms.py", line 59, in populate_obj
field.populate_obj(dummy, name)
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/wtforms/fields/core.py", line 823, in populate_obj
field.populate_obj(fake_obj, 'data')
File "/Users/xxx/.virtualenvs/wtformstest/lib/python2.7/site-packages/wtforms/fields/core.py", line 699, in populate_obj
raise TypeError('populate_obj: cannot find a value to populate from the provided obj or input data/defaults')
TypeError: populate_obj: cannot find a value to populate from the provided obj or input data/defaults