我在python 3.6.5中使用了flask sqlAlchemy,到目前为止,还不能通过调用
__init__()
. 我的代码如下:
'''
file: models.py
'''
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Network(db.Model):
__tablename__ = 'network'
id = db.Column(db.Integer, primary_key=True)
baud_rate = db.Column(db.Integer)
def __init__(**kwargs):
super(Network, self).__init__(**kwargs)
尝试实例化网络对象会导致错误:
>>> n = Network(baud_rate=300)
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: __init__() takes 0 positional arguments but 1 was given
这有点奇怪,因为我正在使用
the recipe given in the Flask-SQLAlchemy documentation
:
如果出于任何原因决定重写构造函数,请确保
继续接受**Kwargs,并用它们调用超级构造函数
**kwargs为了保护这种行为:
class Foo(db.Model):
# ...
def __init__(**kwargs):
super(Foo, self).__init__(**kwargs)
# do custom stuff
因为我使用的是python 3.6,所以我想我应该把调用升级到
super()
,如:
def __init__(**kwargs):
super().__init__(**kwargs)
…但这没什么区别。