On 5/6/15 9:43 PM, Anton wrote:

I decided to use to an event based solution, but I did not want to duplicate default values in the listener function, so I trying to do it in the following way:

|
@event.listens_for(MyModel,'init')
definit(target,args,kwargs):
    target_type =type(target)
forattr_name in('col1','col2'):
        attr_default =getattr(target_type,attr_name).default
ifattr_default isNone:
continue
elifattr_default.is_callable:
            kwargs.setdefault(attr_name,attr_default.arg())
elifattr_default.is_scalar:
            kwargs.setdefault(attr_name,attr_default.arg)
else:
raiseAttributeError("Unsupported default type of column {}".format(attr_name))

|

So far it works for my need, except 2 issues:
1) It does not work with Sequential or Clause defaults, which I don't have need for anyway. 2) It completely relies on ColumnDefault internals and partially duplicates logic from DefaultExecutionContext._exec_default()

I am wondering if there is a better solution to it?

Well it's already weird that you're defining both "default" and "server_default" at the same time on the Column. I'm not actually sure what effect that would have and am surprised it doesn't fail in some way...or at least renders the "server_default" more or less unused.

If you're just defining defaults as constants I'd think you only need "default.arg" and that's it. if it might be callable, the Python builtin callable() will do that for you, is_callable is just a shortcut to having to call that internally. Those are all non-underscored attributes so they are public and safe to use.


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to