Is it possible to use FormAlchemy without binding a session?

23 views
Skip to first unread message

rokfor

unread,
Jan 30, 2009, 4:44:24 AM1/30/09
to FormAlchemy
Hi,

I am using the models of sqlalchemy in a remote environment (pylons
web development) and call the functions with jsonrpc remotely to save
in the origin server (Has the models and db).
In Pylons environment, how is it possible to use the sqlalchemy models
without binding a session (no connection possible to the db) ?
FieldSet(User()) gives the error: Exception: No session found. Either
bind a session explicitly ...

Thanks in advance.

Suha

------------------------------------------------------------------------------------------
import sqlalchemy as sa
from sqlalchemy.orm import relation, backref
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
__tablename__ = 'users'

id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(12))
fullname = sa.Column(sa.Unicode(40))
password = sa.Column(sa.Unicode(20))

Gaël Pasgrimaud

unread,
Jan 30, 2009, 4:57:56 AM1/30/09
to forma...@googlegroups.com
Well, yes.

I've added a simple exemple in the docs (only available at /current for now):

http://docs.formalchemy.org/current/pylons_sample.html

Hope this help

Suha Onay

unread,
Jan 30, 2009, 6:48:44 AM1/30/09
to forma...@googlegroups.com
Hi,

>> c.fs = User.bind(record, data=request.POST or None)
gives the error: AttributeError: type object 'User' has no attribute 'bind'

The class User is defined in my previous message.
Can you give the definition of the model Foo? How is it binded with sqlalchemy?

Suha

Gaël Pasgrimaud

unread,
Jan 30, 2009, 6:55:54 AM1/30/09
to forma...@googlegroups.com
On Fri, Jan 30, 2009 at 12:48 PM, Suha Onay <suha...@gmail.com> wrote:
>
> Hi,
>
>>> c.fs = User.bind(record, data=request.POST or None)
> gives the error: AttributeError: type object 'User' has no attribute 'bind'
>
> The class User is defined in my previous message.
> Can you give the definition of the model Foo? How is it binded with sqlalchemy?
>

User must be a FieldSet

try:

User = FieldSet(model.User)

then:

fs = User.bind(model.User(), ...)

You can have a look at the source of the app used for testing:

http://code.google.com/p/formalchemy/source/browse/trunk/adminapp/adminapp/

Suha Onay

unread,
Jan 30, 2009, 7:19:14 AM1/30/09
to forma...@googlegroups.com
Hi again,

My Code is:

>>from formalchemy import FieldSet
>>fs_User = FieldSet(User)
>>record = User()
>>c.fs = fs_User.bind(record, data=request.POST or None)
>>fs_User.render()
gives the error: Exception: No session found. Either bind a session
explicitly, or specify relation options manually so FormAlchemy
doesn't try to autoload them.

In your example, you used the sessions defined by meta.Session.
As I said, i am not going to save or load data from db using
sqlalchemy, only want to use the models designed for sa to generate
html codes.

Suha

Gaël Pasgrimaud

unread,
Jan 30, 2009, 7:40:12 AM1/30/09
to forma...@googlegroups.com
On Fri, Jan 30, 2009 at 1:19 PM, Suha Onay <suha...@gmail.com> wrote:
>
> Hi again,
>
> My Code is:
>
>>>from formalchemy import FieldSet
>>>fs_User = FieldSet(User)
>>>record = User()
>>>c.fs = fs_User.bind(record, data=request.POST or None)
>>>fs_User.render()
> gives the error: Exception: No session found. Either bind a session
> explicitly, or specify relation options manually so FormAlchemy
> doesn't try to autoload them.
>
> In your example, you used the sessions defined by meta.Session.
> As I said, i am not going to save or load data from db using
> sqlalchemy, only want to use the models designed for sa to generate
> html codes.

It works for me in a ipython shell:

$ import sqlalchemy as sa
$ from sqlalchemy.orm import relation, backref
$ from sqlalchemy.ext.declarative import declarative_base
$
$ Base = declarative_base()
$
$ class User(Base):
$ __tablename__ = 'users'
$ id = sa.Column(sa.Integer, primary_key=True)
$ name = sa.Column(sa.Unicode(12))
$ fullname = sa.Column(sa.Unicode(40))
$ password = sa.Column(sa.Unicode(20))
$
$
$ from formalchemy import FieldSet
$ fs = FieldSet(User())
$ fs.render()
<25> '\n\n<div>\n <label class="field_opt"
for="User--name">Name</label>\n <input id="User--name" maxlength="12"
name="User--name" type="text" />\n</div>\n<script
type="text/javascript">\n//<![CDATA[\ndocument.getElementById("User--name").focus();\n//]]>\n</script>\n<div>\n
<label class="field_opt" for="User--fullname">Fullname</label>\n
<input id="User--fullname" maxlength="40" name="User--fullname"
type="text" />\n</div>\n<div>\n <label class="field_opt"
for="User--password">Password</label>\n <input id="User--password"
maxlength="20" name="User--password" type="text" />\n</div>\n'

$ fs = FieldSet(User)
$ fs.bind(User()).render()
<27> '\n\n<div>\n <label class="field_opt"
for="User--name">Name</label>\n <input id="User--name" maxlength="12"
name="User--name" type="text" />\n</div>\n<script
type="text/javascript">\n//<![CDATA[\ndocument.getElementById("User--name").focus();\n//]]>\n</script>\n<div>\n
<label class="field_opt" for="User--fullname">Fullname</label>\n
<input id="User--fullname" maxlength="40" name="User--fullname"
type="text" />\n</div>\n<div>\n <label class="field_opt"
for="User--password">Password</label>\n <input id="User--password"
maxlength="20" name="User--password" type="text" />\n</div>\n'

This is with SQLAlchemy 0.5.0 and FormAlchemy 1.1

Suha Onay

unread,
Jan 30, 2009, 8:09:10 AM1/30/09
to forma...@googlegroups.com
Yes you are right.

This code works>>>>>>>>>>>>>>>>>>>>>>>>>>
import sqlalchemy as sa
from sqlalchemy.orm import relation, backref
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
__tablename__ = 'users'

id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(12), info={'label': u'Kod Adı'})
fullname = sa.Column(sa.Unicode(40), info={'label': u'Adı ve Soyadı'})
password = sa.Column(sa.Unicode(20), info={'label': u'Şifre'})

def __repr__(self):
return "<User([%s] '%s','%s', '%s')>" % (self.id, self.name,
self.fullname, self.password)

from formalchemy import FieldSet
fs = FieldSet(User())
print fs.render()

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

And this does not >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from deneme.lib.base import BaseController, render
import deneme.lib.app_globals as app_globals

from formalchemy import FieldSet
from deneme.model.test1.base.models.objects import User

log = logging.getLogger(__name__)

class UsersController(BaseController):
requires_auth = True

def index(self):

fs_User = FieldSet(User())
print '===>>>', fs_User.render()

Exception: No session found. Either bind a session explicitly, or
specify relation options manually so FormAlchemy doesn't try to
autoload them.

......
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<

This is with SQLAlchemy 0.5.0, FormAlchemy 1.1.1 and Pylons 0.9.7rc4

What am i doing wrong?


Suha

On Fri, Jan 30, 2009 at 14:40, Gaël Pasgrimaud

Gaël Pasgrimaud

unread,
Jan 30, 2009, 8:15:04 AM1/30/09
to forma...@googlegroups.com
Grumf. Try with FieldSet(User).render()
FA will instanciate a User() for you AFAIK.

Jonathan ellis

unread,
Jan 30, 2009, 8:15:30 AM1/30/09
to forma...@googlegroups.com
You probably have relations declared on your User model that FA is
trying to load.

-Jonathan

Suha Onay

unread,
Jan 30, 2009, 8:46:14 AM1/30/09
to forma...@googlegroups.com
Ups. Yes u r right.
There is one other model referenced to this one:

class Address(Base):
__tablename__ = 'addresses'

id = sa.Column(sa.Integer, primary_key=True)
email_address = sa.Column(sa.Unicode(50), nullable=False)
user_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'))

user = relation(User, backref=backref('addresses', order_by=id))

def __init__(self, email_address):
self.email_address = email_address

def __repr__(self):
return "<Address('%s')>" % self.email_address

If this reference is deleted, it works fine.
Then, how can I generate forms for the User model related with Adress
model without the session error?

Suha

Gaël Pasgrimaud

unread,
Jan 30, 2009, 8:52:09 AM1/30/09
to forma...@googlegroups.com
On Fri, Jan 30, 2009 at 2:46 PM, Suha Onay <suha...@gmail.com> wrote:
>
> Ups. Yes u r right.
> There is one other model referenced to this one:
>
> class Address(Base):
> __tablename__ = 'addresses'
>
> id = sa.Column(sa.Integer, primary_key=True)
> email_address = sa.Column(sa.Unicode(50), nullable=False)
> user_id = sa.Column(sa.Integer, sa.ForeignKey('users.id'))
>
> user = relation(User, backref=backref('addresses', order_by=id))
>
> def __init__(self, email_address):
> self.email_address = email_address
>
> def __repr__(self):
> return "<Address('%s')>" % self.email_address
>
> If this reference is deleted, it works fine.
> Then, how can I generate forms for the User model related with Adress
> model without the session error?
>

Try this:

fs = FieldSet(User)
fs.configure(exclude=[fs.addresses])

You can do the same with Address and associate both in one form since
FA's fieldets dont generate a <form /> tag.

Suha Onay

unread,
Jan 30, 2009, 8:55:44 AM1/30/09
to forma...@googlegroups.com
Thank u all.
It works now :)

Suha


2009/1/30 Gaël Pasgrimaud <gael.pa...@gmail.com>:

Jonathan ellis

unread,
Jan 30, 2009, 9:00:33 AM1/30/09
to forma...@googlegroups.com
Or ypu can use addresses as long as you explicitly specify an options
list.

-Jonathan

On Jan 30, 2009, at 7:52 AM, Gaël Pasgrimaud
Reply all
Reply to author
Forward
0 new messages