On 08/05/2011 10:46 PM, Mark Erbaugh wrote:
This is more of a Python issue than a SA issue, but I had trouble getting this to
work. I did, but the code seems a little awkard to me<sigh>. In addition to
the requirements already, I also wanted toe default value to be a class level
'constant'. The problem, as I see it, is that since the class definition isn't
complete, it's namespace isn't avaialble. Since the default value 'constant' is a
class data member, it would make sense if the function were a @classmethod, but I
couldn't get python to accept:
class Table(Base):
...
DEFAULT = 2
@classmethod
def CustomColumn(cls):
return Column(Integer, default=DEFAULT)
that should be cls.DEFAULT
...
field1 = CustomColumn()
Python complained 'classmethod object is not callable' on the last line above.
You can only call a class method on a class. In this case that would be
Table.CustomColumn(). However since the Table class is not available at
this point you can't do that. You can do this sort of thing with
metaclasses, but I would not recommend going down that paht.
What I finally ended up with that works is:
class Table(Base):
...
DEFAULT = 2
def CustomColumn(default=DEFAULT):
return Column(Integer, default=default)
...
field1 = CustomColumn()
That looks like a pretty good solution.
Wichert.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.