pallets-eco / wtforms-appengine

WTForms integration for Google App Engine
https://wtforms-appengine.readthedocs.io
Other
42 stars 16 forks source link

KeyPropertyField fails #6

Closed r00tat closed 8 years ago

r00tat commented 10 years ago

I'm using model_form to generate a form for a class with a KeyProperty. If the KeyProperty field is empty, render works fine. If there is already a key, it fails.

ERROR    2014-11-04 11:01:40,258 webapp2.py:1552] 'Key' object has no attribute 'key'
Traceback (most recent call last):
  File "/Applications/developing/gce/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/developing/gce/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/developing/gce/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/developing/gce/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/developing/gce/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/developing/gce/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
INFO     2014-11-04 11:01:40,265 module.py:391] [cloud-dns] Detected file changes:
      lib/wtforms_appengine
    return method(*args, **kwargs)
  File "    app/base.py", line 33, in check_login
    return handler(self, *args, **kwargs)
  File "    app/account/account_handler.py", line 56, in get
    form.organization()
  File "    lib/wtforms/fields/core.py", line 149, in __call__
    return self.meta.render_field(self, kwargs)
  File "    lib/wtforms/meta.py", line 53, in render_field
    return field.widget(field, **render_kw)
  File "    lib/wtforms/widgets/core.py", line 280, in __call__
    for val, label, selected in field.iter_choices():
  File "    lib/wtforms_appengine/fields.py", line 152, in iter_choices
    yield (key, label, (self.data.key == obj.key) if self.data else False)
AttributeError: 'Key' object has no attribute 'key'

The yield line is wrong. self.data is already a key object and not the Model itself. To fix this issue, line 152 in wtforms_appengine/fields.py should look like this:

yield (key, label, (self.data == obj.key) if self.data else False)
savraj commented 9 years ago

Hey, thanks for this. I'm hitting the same issue. Any reason this hasn't been accepted into the main branch yet?

savraj commented 9 years ago

Ok, I actually ran into a slightly different problem. I changed line 165 if self.data.key == obj.key: to if self.data == obj.key: and my code worked.

savraj commented 9 years ago

A better solution is excluding the field in question from the form. So when I make a form, I say myform = model_form(MyNDBModel,exclude=['fieldthattakeskeyproperty']) that means you won't hit this issue.

r00tat commented 9 years ago

@savraj no that's not a solution, because I want this field in the form. I.e. you got a Book and a Author model. I want to be able to select the Author and not just ignore it.

savraj commented 9 years ago

Ah yes, sorry about that.

shday commented 9 years ago

Pull request https://github.com/wtforms/wtforms-appengine/pull/8 seems to include both changes mentioned above.

mikelambert commented 8 years ago

I've recently taken on maintainership of this repo, and have merged in pull request #8, so I'm going to close this issue as fixed.