FormAlchemy/formalchemy — Issue #20: hybrid_properties
Author: marplatense
Created: 2011-10-28 19:51:15+00:00
State: open

I was under the impression [issue 11](https://github.com/FormAlchemy/formalchemy/pull/11) would include hybrid_properties but I cannot make them work. Consider the following example where *gender* is a synonym for *_gender*:

from formalchemy import Column
from formalchemy.forms import FieldSet
from sqlalchemy import Table, Unicode, Integer, create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import sessionmaker
session = sessionmaker()
Session = session()
engine = create_engine('postgresql://usr:pass@localhost/db', echo=True)
Session.configure(bind=engine)
m = MetaData()
m.bind = engine
base = declarative_base(metadata=m)

class Test(base):
__tablename__ = "example"
id = Column(Integer, autoincrement=True, primary_key=True)
name = Column(Unicode(100), nullable=False)
_gender = Column(Unicode(1), nullable=False, default=True)
@hybrid_property
def gender(self):
return self._gender
@gender.setter
def gender(self, value):
if value.upper() not in ('F', 'M'):
raise(Exception(u'Gender must be F or M'))
else:
self._gender = value.upper()

m.create_all()

fs1 = FieldSet(Test())
fs1.configure(include=[fs1.name, fs1.gender])
fs1.render()

When I execute it, I'm getting:

Traceback (most recent call last):
File "", line 1, in
import property_fa as f
File "property_fa.py", line 35, in
fs1.configure(include=[fs1.name, fs1.gender])
File "/home/mariano/Code/example/env/lib/python2.7/site-packages/FormAlchemy-1.4.1-py2.7.egg/formalchemy/forms.py", line 773, in __getattr__
raise AttributeError(attrname)
AttributeError: gender

If I use *_gender* as the included field, of course it works however it bypasses my property control.

TIA,
Mariano