假设我在SQLALchemy中有3个类:Topic
,,,Tag
,,,Tag_To_Topic
...
是否有可能写一些类似于:
new_topic = Topic("new topic")
Topics.tags = ['tag1', 'tag2', 'tag3']
在标签表中自动插入“tag1”、“tag2”和“tag3”,并在new_topic
这三个标签Tag_To_Topic
。
到目前为止,我还没有弄清楚如何做到这一点,因为很多很多的关系。(如果它是一对多的,这将是非常容易的,SQLAlchemy默认情况下已经这样做了。但这是多对多的。)
这个是可能的吗?
可以试试这个:
>>> import array
>>> array
<module 'array' (built-in)>
>>> class A(array.array):
def __init__(self, *args):
super(array.array, self).__init__(*args)
print "init is fine for objects implemented in C"
>>> a=A('c')
init is fine for objects implemented in C
>>>
将看到相同的行为子类int、str等。
>>> import datetime
>>> class D(datetime.date):
def __new__(cls, year):
return datetime.date.__new__(cls, year, 1, 1)
>>> D(2008)
D(2008, 1, 1)