python - SQLAlchemy class __init__() meets takes at least 5 non-keyword arguments error -
i wrote code
user class sqlalcemy
##############models.py class user(base,usermixin): __tablename__ = 'users' id = column(integer, sequence('seq_user_id'), primary_key=true) name = column(string(50), unique=true, index = true, nullable = false) email = column(string(120), unique=true, index = true, nullable = false) password = column(string(128), nullable = false) registerdate = column(date, nullable = false) def __init__(self, name, email, password, registerdate): self.name = name self.email = email self.password = password self.registerdate = datetime.datetime.now() def __repr__(self): return '<user %r>' % (self.name) def is_authenticated(self): return true def is_active(self): return true def is_anonymous(self): return false def get_id(self): return unicode(self.id)
then wrote code create test user
############## frontend.py @app.route('/createuser') def createuser(): user_jacky = user(name='jacky',email='jacky@test.com',password=md5.new('123').hexdigest()) db_session.add(user_jacky) db_session.commit() return redirect(url_for('index'))
when access http://test.com/createuser, system told me "typeerror: init() takes @ least 5 non-keyword arguments (4 given)". read document of sqlalchemy again, that's same code, not need pass value self. happend?
the problem __init__
expects pass in registerdate
, aren't providing 1 when __init__
called through line:
user(name='jacky',email='jacky@test.com',password=md5.new('123').hexdigest())
if registerdate
should now()
, can leave out parameter:
def __init__(self, name, email, password): self.name = name self.email = email self.password = password self.registerdate = datetime.datetime.now()
if want able provide registerdate
, should default now()
, can this:
def __init__(self, name, email, password, registerdate=none): self.name = name self.email = email self.password = password self.registerdate = registerdate or datetime.datetime.now()
Comments
Post a Comment