kwargs are ignored when creating an object

Issue #351 resolved
Former user created an issue

I'm pretty sure following used to work but not anymore. Is it an intentended change? If it is I hope the decision can be reversed.

meta = DynamicMetaData()
people = Table('people', meta,
        Column('id', Integer, primary_key=True),
        Column('fname', String(30)),
        Column('lname', String(30)),
        )

class Person(object): pass
mapper(Person, people)

kw = dict(fname='John', lname='Smith')
p1 = Person(**kw)
p2 = Person()
p2.fname = 'John'
p2.lname = 'Smith'

assert p1.fname == p2.fname

The workaround is just too ugly :-)

p1 = Person()
[k, v) for k,v in d.items()](setattr(p1,)

Comments (2)

  1. Mike Bayer repo owner

    its too much of an assumption to make for SA to guess how youd like your object's constructor to function. SA's job is SQL generation, not class construction. you can get this behavior back if you use the assignmapper extension module:

       assign_mapper(sessioncontext, Person, people)
    

    (or just define a superclass for your objects with a one liner constructor....)

       class SuperClass(object):
           def __init__(self, **kwargs):
               setattr(self, k, v) for k, v in kwargs.iteritems()
    
       class Person(SuperClass) ....
    
  2. Log in to comment