pombreda / formalchemy

Automatically exported from code.google.com/p/formalchemy
MIT License
0 stars 0 forks source link

FieldSet customization breaks admin on second insert in Pylons #124

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create default pylons fa project
paster create -t pylons_fa TestFA template_engine=mako sqlalchemy=True 
admin_controller=True

2. Uncomment foo_table, class foo and the mapper in the model
## Non-reflected tables may be defined and mapped at module level
foo_table = sa.Table("Foo", meta.metadata,
    sa.Column("id", sa.types.Integer, primary_key=True),
    sa.Column("bar", sa.types.String(255), nullable=False),
)

class Foo(object):
    pass

orm.mapper(Foo, foo_table)

3. Create the table in database, and start the server
-> Going to /admin, you can add as many Foo as you like

4. Create a custom FieldSet in forms/__init__.py
just uncomment Foo = FieldSet(model.Foo)

What is the expected output? 
No change from step 3

What do you see instead?
You've got an exception (pasted below) when trying to insert as second Foo in 
admin.

What version of the product are you using? On what operating system?
Python 2.6.3 (64 bit, UCS-2) on Mac OS X 10.6.1
FormAlchemy: 1.3.1
SA: 0.5.6
Pylons: 0.9.7

Here is the traceback

File 
'xxx/site-packages/FormAlchemy-1.3.1-py2.6.egg/formalchemy/ext/pylons/controller
.py', line 309 in new
  fs = fs.bind(session=self.Session())
File 'xxx/site-packages/FormAlchemy-1.3.1-py2.6.egg/formalchemy/base.py', line 
333 in bind
  model = fields._pk(self.model) is None and type(self.model) or self.model
File 'xxx/site-packages/FormAlchemy-1.3.1-py2.6.egg/formalchemy/fields.py', 
line 587 in _pk
  return _pk_one_column(instance, columns[0])
File 'xxx/site-packages/FormAlchemy-1.3.1-py2.6.egg/formalchemy/fields.py', 
line 560 in _pk_one_column
  attr = getattr(instance, column.key)
File 
'xxx/site-packages/SQLAlchemy-0.5.6-py2.6.egg/sqlalchemy/orm/attributes.py', 
line 158 in __get__
  return self.impl.get(instance_state(instance), instance_dict(instance))
File 
'xxx/site-packages/SQLAlchemy-0.5.6-py2.6.egg/sqlalchemy/orm/attributes.py', 
line 377 in get
  value = callable_()
File 'xxx/site-packages/SQLAlchemy-0.5.6-py2.6.egg/sqlalchemy/orm/state.py', 
line 185 in __call__
  attr.impl.key in unmodified
File 'xxx/site-packages/SQLAlchemy-0.5.6-py2.6.egg/sqlalchemy/orm/mapper.py', 
line 1845 in _load_scalar_attributes
  "attribute refresh operation cannot proceed" % (state_str(state)))
UnboundExecutionError: Instance <Foo at 0x103779f90> is not bound to a Session; 
attribute refresh operation cannot proceed

Original issue reported on code.google.com by antonin....@gmail.com on 7 Nov 2009 at 10:27

GoogleCodeExporter commented 9 years ago
I'm new to FormAlchemy, so this may be wrong, but it seems that the SA object 
(Foo) used as model is the one 
being used for create the first time. This makes it 'SA managed' when it should 
not be.

By changing line 279 of formalchemy/ext/pylons/controller.py from
fs = fs.bind(fs.model, data=request.POST, session=self.Session())
in
fs = fs.bind(type(fs.model), data=request.POST, session=self.Session())

my (simplistic) test works. But a proper fix may need to check what fs.model is 
before doing that.

Original comment by antonin....@gmail.com on 8 Nov 2009 at 12:51

GoogleCodeExporter commented 9 years ago
self.get_model() always return the class. Now fixed. Thanks

Original comment by gael.pas...@gmail.com on 8 Nov 2009 at 11:55