TypeError:__ init __()只需2个参数(给定1个)Flask

时间:2017-02-02 15:01:45

标签: python flask-sqlalchemy

我知道需要提供默认='值'对于这种情况。我已经通过StackOverFlow咨询了解决方案,但无法摆脱这个错误。有或没有默认值我从管理表单保存时收到此错误。错误在init函数的Packeges模块中。请指教。 以下是我的代码:

class Packeges(db.Model):
    __tablename__ = 'packages'
    id = db.Column(db.Integer, primary_key=True)
    package_name = db.Column(db.String(30), default='Add Package Here')
    def __init__(self, package_name):
        self.package_name = package_name


admin.add_view(ModelView(Packeges, db.session))

class UserSubscription(db.Model):
    __tablename__ = 'user_subscription'
    id = db.Column(db.Integer, primary_key=True)
    user_profile = db.Column(db.String(80), unique=True)
    package_name = db.Column(db.Integer, db.ForeignKey(Packeges.id))
    packages = db.relationship(Packeges, backref='package')
    timestamp = db.Column(db.DateTime)
    expiry = db.Column(db.DateTime)

    def __init__(self, user_profile, package_name, timestamp, expiry):
        self.user_profile = user_profile
        self.package_name = package_name
        self.timestamp = timestamp
        self.expiry = expiry
admin.add_view(ModelView(UserSubscription, db.session))

回溯:

 TypeError
TypeError: __init__() takes exactly 2 arguments (1 given)

Traceback (most recent call last)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask_admin\base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask_admin\base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask_admin\model\base.py", line 1920, in create_view
model = self.create_model(form)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask_admin\contrib\sqla\view.py", line 1028, in create_model
Open an interactive python shell in this frameif not self.handle_view_exception(ex):
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask_admin\contrib\sqla\view.py", line 1011, in handle_view_exception
return super(ModelView, self).handle_view_exception(exc)
File "C:\Users\AliKhan\flask_ecom\env\lib\site-packages\flask_admin\contrib\sqla\view.py", line 1022, in create_model
model = self.model()

1 个答案:

答案 0 :(得分:2)

尝试如下:

def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)