Trouble with second association on a form

3 views
Skip to first unread message

Andriod

unread,
Aug 14, 2008, 5:41:36 PM8/14/08
to FormAlchemy
I'm having trouble with getting formalchemy to deal with the second
association on a form. What seems to be happening is that my model
object gets a primary key between validation and syncing that field.
It's almost certainly an effect of SQLAlchemy 0.5, but one of the
default settings so I'd like to implement handling for it in
FormAlchemy if you can advise, here's the troublesome code:

def create(self):
"""POST /campaigns: Create a new item."""
# url_for('campaigns')
newCampaign=model.Campaign()
c.fs = FieldSet(model.Campaign,session=model.Session)
c.fs.rebind(newCampaign,
data=request.params,session=model.Session)
if(c.fs.validate()):
c.fs.sync()

and here's the error I get through pylons:

Module sushibar.controllers.campaigns:54 in update
return self.create()
Module sushibar.controllers.campaigns:28 in create
c.fs.sync()
Module formalchemy.forms:288 in sync
AbstractFieldSet.sync(self)
Module formalchemy.base:346 in sync
field.sync()
Module formalchemy.fields:723 in sync
setattr(self.model, self.name, self._deserialize())
Module formalchemy.fields:763 in _deserialize
return self.renderer.deserialize()
Module formalchemy.fields:106 in deserialize
return self._deserialize(self._serialized_value())
Module formalchemy.fields:90 in _serialized_
return params.getone(self.name)
Module paste.util.multidict:281 in getone
return self._decode_value(self.multi.getone(key))
Module paste.util.multidict:68 in getone
raise KeyError('Key not found: %r' % key)
<type 'exceptions.KeyError'>: "Key not found: 'Campaign-1-AccountID'"

What seems to be happening is that a previous field is setting up an
association on the object so Form Alchemy can't find the data it needs
in ["Campaign--AccountID"]. What I'd like to do is have FormAlchemy
fall back and check "{modelType}--{fieldName}" when it can't find a
field of "{modelType}-{primary_key}-{fieldName}" but with that name
actually coming from the renderer :I'm not sure where that should
properly be coded in, any suggestion? Should I be handling this
completely differently?

- Andy Fundinger

Jonathan Ellis

unread,
Aug 14, 2008, 6:40:44 PM8/14/08
to forma...@googlegroups.com
[On Thu, Aug 14, 2008 at 3:41 PM, Andriod <and...@gmail.com> wrote:
> What seems to be happening is that a previous field is setting up an
> association on the object so Form Alchemy can't find the data it needs
> in ["Campaign--AccountID"]. What I'd like to do is have FormAlchemy
> fall back and check "{modelType}--{fieldName}" when it can't find a
> field of "{modelType}-{primary_key}-{fieldName}" but with that name
> actually coming from the renderer :I'm not sure where that should
> properly be coded in, any suggestion? Should I be handling this
> completely differently?

The root cause is probably a flush populating that pk before FA
expects it, so as a workaround you can try turning off autoflush. But
I'm not sure what would be causing a flush during sync; if you can
make a self-contained test illustrating the problem like the one
attached here http://code.google.com/p/formalchemy/issues/detail?id=21&can=1
I'd be glad to try to figure that out.

That said, here's some other improvements:
- the constructor takes the same arguments as rebind, so it's silly
to call one right after the other. just pass what you want to the
constructor.
- FA will take care of creating an empty object w/ the default
constructor to sync to, so unless you need extra args to your
constructor there's no need to manually do that
- if you're using scoped_session (and i think you are) there's no
need to pass sessions around

def create(self):
"""POST /campaigns: Create a new item."""

c.fs = FieldSet(model.Campaign, data=request.params)
if(c.fs.validate()):
c.fs.sync()
model.Session.commit()

-Jonathan

Reply all
Reply to author
Forward
0 new messages