[Chicago] how to pass a dictionary to sqlobject

Stephen Crim stephencrim at gmail.com
Sat Aug 11 15:51:16 CEST 2007

THe problem with your approach is that the standard __init__ method is
relied upon very heavily by SQLObject to split apart what you're
doing. You can't pass in a dict and just have the dict keys become
keyword arguments, even though the **arg actually does that. * and **
are special arguments that are only generated by python, keyword
arguments go into one bucket, non keyword arguments another. At least,
I don't know of a way to trick it into doing that without overriding a
bunch of other methods, which I try to avoid doing because I hate
straying too far from the path.

Your best option might be to override the __init__ method in
SQLObject, accepting *args, **kwargs as arguments, then parse out the
dict that's passed in as a member of args, the build out a call to
SQLObject.__init__( self, arg=val, arg=val) as a string which you can

here's how the args work:

>>> def argtest( *args, **kwargs ):
...   print args
...   print kwargs
>>> argtest({'a': 1, 'b': 2})
({'a': 1, 'b': 2},)
>>> argtest({'a': 1, 'b': 2}, a=1, b=2)
({'a': 1, 'b': 2},)
{'a': 1, 'b': 2}

here's an example of the __init__method:
class MyClass (SQLObject):
    prop = IntCol()
    prop2 = IntCol()
    def __init__( self, *args, **kwargs ):
        d = [i for i in args if type(i) == type({})][0] # don't forget
to catch this exception
        l  = ['%s = %s' % (k, str(v)) for k,v in d.items()]
        s = "SQLObject.__init__(self, %s)" % ', '.join(l)
        exec(s) # this will call the __init__  method and actually
build your functions

then you should be able to call MyClass({'prop': 1, 'prop2': 2}). Be
wary of the squashing to a string that happens, and do take care to do
proper argument validation before relying on this in a publicly
accessible system. I haven't tested this approach with SQLObject, but
i know that __init__ does some voodoo, so you may need to use a helper
function to accomplish this instead and leave your class defs as they

Hope that helps!


> Message: 1
> Date: Fri, 10 Aug 2007 13:46:11 -0500
> From: "Lukasz Szybalski" <szybalski at gmail.com>
> Subject: [Chicago] how to pass a dictionary to sqlobject
> To: "The Chicago Python Users Group" <chicago at python.org>
> Message-ID:
>         <804e5c70708101146l717688b2gffc172b83ad8ccc9 at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
> Hello,
> I have a problem when Im trying to save a form. My form when submitted
> gives me a a dictionary with field names and values.
> If my sqlobject class is
> class App(SQLObject):
>    idName = "App_Sid"
>    class sqlmeta:
>        fromDatabase = True
>        table="App"
> How do i pass my dictionary kwargs={'App_Sid':1,'First_name':'lucas'} ????
> Doing something like this:
> App(**kwargs)
> Gives me an error like:
> File "c:\python25\lib\site-packages\SQLObject-0.9.0-py2.5.egg\sqlobject\col.py",
> line 596, in from_python
>    (self.name, type(value), value), value, state)
> Invalid: expected an int in the IntCol 'APPSID', got <type 'str'> '' instead
> How do you guys pass a list of key and values to sqlobject?
> Thanks,
> Lucas

More information about the Chicago mailing list