我正在尝试使用SQLAlchemy + Flask将一个项目添加到我的数据库中,但是一直出现错误。
class users(db.Model):
userid = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
email = db.Column(db.String(120))
password = db.Column(db.String(80))
duguns = db.relationship('dugun', backref='whuser', lazy='dynamic')
yorums = db.relationship('yorum', backref='whuser', lazy='dynamic')
genels = db.relationship('genel', backref='whuser', lazy='dynamic')
bilgis = db.relationship('bilgi', backref='whuser', lazy='dynamic')
class bilgi(db.Model):
kisiselid = db.Column(db.Integer, primary_key=True)
userid = db.Column(db.Integer, db.ForeignKey('users.userid'))
gelinAdi = db.Column(db.String(80))
damatAdi = db.Column(db.String(80))
gelinFoto = db.Column(db.String(80))
damatFoto = db.Column(db.String(80))
gelinBio = db.Column(db.String(120))
damatBio = db.Column(db.String(80))
@app.route("/admin", methods=["GET","POST"])
@login_required
def admin():
form = KisiForm(request.form)
if request.method == "POST":
gelinAdi = form.gelinAdi.data
gelinFoto = form.gelinFoto.data
gelinBio = form.gelinBio.data
damatAdi = form.damatAdi.data
damatFoto = form.damatFoto.data
damatBio = form.damatBio.data
whuser = session["username"]
kisi = bilgi(whuser = whuser, gelinAdi = gelinAdi, gelinFoto = gelinFoto, gelinBio = gelinBio, damatAdi = damatAdi, damatFoto = damatFoto, damatBio = damatBio)
db.session.add(kisi)
db.session.commit()
return redirect(url_for("admin"))
return render_template("admin/index.html",form=form)
那是代码。当我运行Python文件时,出现此错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/bozkurt/Desktop/davetiye/site.py", line 70, in decorated_function
return f(*args, **kwargs)
File "/Users/bozkurt/Desktop/davetiye/site.py", line 172, in admin
kisi = bilgi(whuser = whuser, gelinAdi = gelinAdi, gelinFoto = gelinFoto, gelinBio = gelinBio, damatAdi = damatAdi, damatFoto = damatFoto, damatBio = damatBio)
File "<string>", line 4, in __init__
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 417, in _initialize_instance
manager.dispatch.init_failure(self, args, kwargs)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 414, in _initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py", line 700, in _declarative_constructor
setattr(self, k, kwargs[k])
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 813, in set
value = self.fire_replace_event(state, dict_, value, old, initiator)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 833, in fire_replace_event
state, value, previous, initiator or self._replace_token)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 1181, in emit_backref_from_scalar_set_event
child_state, child_dict = instance_state(child),\
AttributeError: 'str' object has no attribute '_sa_instance_state'
AttributeError: 'str' object has no attribute '_sa_instance_state
。我该怎么办?
答案 0 :(得分:0)
您已将列定义为反向引用:
bilgis = db.relationship('bilgi', backref='whuser', lazy='dynamic')
表示值they're set to represents an ORM object-不是单个字符串。但是,您确实在创建对象时仅发送了字符串(用户名):
whuser = session["username"]
kisi = bilgi(whuser = whuser, ...)
请确保提供从ORM返回的用户对象:
bilig(whuser=<actual user object from the ORM>, ..)
您可以通过先从ORM提取用户来实现:
user = users.query.filter_by(username=whuser).first()
然后在创建依赖对象时使用该值:
bilig(whuser=user, ..)