Many to many validation

2 views
Skip to first unread message

grassoalvaro

unread,
Jan 14, 2009, 5:27:43 PM1/14/09
to FormAlchemy
Hi,

I can't find any documentation about m2m validation. Here's some code:

workers_professions = Table("workers_professions", meta.metadata,
Column("worker_id", Integer, ForeignKey('workers.id')),
Column("profession_id", Integer, ForeignKey('professions.id'))
)

workers_table = Table("workers", meta.metadata,
Column("id", Integer, primary_key=True)
)

mapper(Worker, workers_table, properties={
'professions': relation(Profession, secondary=workers_professions)
})


If i bind data from form and then make fs.validate() i have error:


File '/usr/lib/python2.5/site-packages/FormAlchemy-1.0.1-py2.5.egg/
formalchemy/forms.py', line 288 in validate
return AbstractFieldSet.validate(self)
File '/usr/lib/python2.5/site-packages/FormAlchemy-1.0.1-py2.5.egg/
formalchemy/forms.py', line 71 in validate
success = field._validate() and success
File '/usr/lib/python2.5/site-packages/FormAlchemy-1.0.1-py2.5.egg/
formalchemy/fields.py', line 546 in _validate
value = self._deserialize()
File '/usr/lib/python2.5/site-packages/FormAlchemy-1.0.1-py2.5.egg/
formalchemy/fields.py', line 985 in _deserialize
for pk in self.renderer.deserialize()]
File '/usr/lib/python2.5/site-packages/FormAlchemy-1.0.1-py2.5.egg/
formalchemy/fields.py', line 540 in query
return self.parent.session.query(*args, **kwargs)
AttributeError: 'NoneType' object has no attribute 'query'

When i type fs.configure(exclude=[fs.professions]) bug disappear by i
this case i can't use my own validation method in options=[]. Any
ideas how to do this?

(sorry for my english)

Jonathan Ellis

unread,
Jan 14, 2009, 5:45:01 PM1/14/09
to forma...@googlegroups.com
Are you running trunk, or the 1.1 release?

That looks like a bug that is fixed in trunk.

-Jonathan

grassoalvaro

unread,
Jan 14, 2009, 6:18:47 PM1/14/09
to FormAlchemy
In 1.1 the same.
In trunk version is other bug:

File '/home/pako/pf/pf/controllers/worker.py', line 60 in
ajax_register
if not fs.validate():
File '/home/pako/pf/formalchemy/forms.py', line 292 in validate
return AbstractFieldSet.validate(self)
File '/home/pako/pf/formalchemy/forms.py', line 71 in validate
success = field._validate() and success
File '/home/pako/pf/formalchemy/fields.py', line 638 in _validate
value = self._deserialize()
File '/home/pako/pf/formalchemy/fields.py', line 1105 in _deserialize
return [self.query(self.relation_type()).get(python_pk(pk)) for pk
in self.renderer.deserialize()]
File '/home/pako/pf/formalchemy/fields.py', line 628 in query
raise Exception("No session found. Either bind a session
explicitly, or specify relation options manually so FormAlchemy
doesn't try to autoload them.")
Exception: No session found. Either bind a session explicitly, or
specify relation options manually so FormAlchemy doesn't try to
autoload them.

On 14 Sty, 23:45, Jonathan Ellis <jbel...@gmail.com> wrote:
> Are you running trunk, or the 1.1 release?
>
> That looks like a bug that is fixed in trunk.
>
> -Jonathan
>

Jonathan Ellis

unread,
Jan 14, 2009, 6:29:03 PM1/14/09
to forma...@googlegroups.com
Did you try passing it a session object like it asked? :)

It's possible that there's a bug, but more likely you're not using a
session-connected mapper, so you do need to pass a session to
[re]bind.

-Jonathan

grassoalvaro

unread,
Jan 14, 2009, 6:44:59 PM1/14/09
to FormAlchemy
Dammit! Need some sleep i think ;-)
Thank you, but there is another problem:

Data from request.POST:
UnicodeMultiDict([('Worker--professions', u'2'), ('Worker--
professions', u'3')])

def test(val):
print 'text', val

fs.configure(
options=[
fs.professions.validate(test),
]
)

And here output from test() function:

text [None, None]

Any idea for this? ;-)

On 15 Sty, 00:29, Jonathan Ellis <jbel...@gmail.com> wrote:
> Did you try passing it a session object like it asked? :)
>
> It's possible that there's a bug, but more likely you're not using a
> session-connected mapper, so you do need to pass a session to
> [re]bind.
>
> -Jonathan
>

Jonathan Ellis

unread,
Jan 14, 2009, 6:47:15 PM1/14/09
to forma...@googlegroups.com
did you call fs.sync()?

grassoalvaro

unread,
Jan 14, 2009, 6:54:08 PM1/14/09
to FormAlchemy
No. Should I?

If i use test() function for other fields, f.e. fs.email it's working
- printing given value.

On 15 Sty, 00:47, Jonathan Ellis <jbel...@gmail.com> wrote:
> did you call fs.sync()?
>

Jonathan Ellis

unread,
Jan 14, 2009, 6:59:27 PM1/14/09
to forma...@googlegroups.com
... you're right, if you're testing against the fs and not your model
sync shouldn't matter.

possibly there is a bug in the multidict handling, what do you get if you pass

data={'Worker--professions': ('2', '3')}

?

-Jonathan

grassoalvaro

unread,
Jan 14, 2009, 7:08:23 PM1/14/09
to FormAlchemy
Hmm.. nothing, there is still [None, None]

On 15 Sty, 00:59, Jonathan Ellis <jbel...@gmail.com> wrote:
> ... you're right, if you're testing against the fs and not your model
> sync shouldn't matter.
>
> possibly there is a bug in the multidict handling, what do you get if you pass
>
> data={'Worker--professions': ('2', '3')}
>
> ?
>
> -Jonathan
>

grassoalvaro

unread,
Jan 14, 2009, 7:14:57 PM1/14/09
to FormAlchemy
And even with data={'Worker--professions': '2'} is [None]

Jonathan ellis

unread,
Jan 14, 2009, 7:48:43 PM1/14/09
to forma...@googlegroups.com
Can you post a complete test case?

-Jonathan

On Jan 14, 2009, at 6:14 PM, grassoalvaro <grasso...@yahoo.com>
wrote:

grassoalvaro

unread,
Jan 15, 2009, 8:12:28 AM1/15/09
to FormAlchemy

worker = Worker()

def test(val):
print 'jojo', val

from formalchemy import FieldSet, validators
fs = FieldSet(Worker)
fs.configure(
include=[
fs.professions,
fs.email,
],
options=[
fs.professions.validate(test),
fs.email.validate(test),
]
)
data={'Worker--email': 'dupa', 'Worker--professions': '2'}
fs.rebind(worker, session=db, data=data)
fs.validate()

db is a meta.Session. Worker is an empty class (mapped with
workers_table - i pasted it above)

output:
jojo [None]
jojo dupa


On 15 Sty, 01:48, Jonathan ellis <jbel...@gmail.com> wrote:
> Can you post a complete test case?
>
> -Jonathan
>
> On Jan 14, 2009, at 6:14 PM, grassoalvaro <grassoalv...@yahoo.com>  

grassoalvaro

unread,
Jan 16, 2009, 5:58:40 PM1/16/09
to FormAlchemy
Och God, i really don't know that formalchemy is selecting objects in
m2m. My professions table was empty, so element with id=2 was None...

Jonathan Ellis

unread,
Jan 16, 2009, 6:09:06 PM1/16/09
to forma...@googlegroups.com
Great, problem solved! :)

-Jonathan
Reply all
Reply to author
Forward
0 new messages