我正在尝试使用 cassandra 并使用 Flask-CQLAlchemy 库编写烧瓶应用程序。当我试图创建我的模型类(学生)的对象时,它给了我一个名为'Student' object has no attribute '_values' 的错误。经历了与我的错误相匹配的多个解决方案,但无法解决这个问题。
以下是包含 python 代码的文件:
from flask import *
from flask_cqlalchemy import CQLAlchemy
app = Flask(__name__)
app.config['CASSANDRA_HOSTS'] = ['127.0.0.1']
app.config['CASSANDRA_KEYSPACE'] = "emp"
db = CQLAlchemy(app)
class Student(db.Model):
uid = db.columns.Integer(primary_key=True)
username = db.columns.Text(required=True)
password = db.columns.Text()
def __init__(self, uid, username, password):
self.uid = uid
self.username = username
self.password = password
@app.route('/')
def show_index():
return render_template('index.html')
@app.route('/add', methods = ['POST', 'GET'])
def add():
if request.method == 'POST':
if not request.form['uid'] or not request.form['username'] or not request.form['password']:
flash('Please enter all the fields', 'error')
else:
student1 = Student(request.form['uid'], request.form['username'], request.form['password'])
student.save()
return render_template("successful.html")
else:
abort(401)
db.sync_db()
if __name__ == '__main__':
app.run(debug = True)
这个是index.html文件
<!DOCTYPE html>
<html lang = "en">
<head><title>index</title></head>
<body>
<form action = "/add" method = "post">
UId: <input type="text" name="uid">
Name: <input type="text" name="username">
Password: <input type="text" name="password">
<input type="submit" value="Submit">
</form>
</body>
</html>
错误日志:
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/sudarshan/python/try/try1.py", line 31, in add
student1 = Student.create(uid=7, username='sud', password='asdfghjkl')
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/cassandra/cqlengine/models.py", line 673, in create
return cls.objects.create(**kwargs)
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/cassandra/cqlengine/query.py", line 981, in create
return self.model(**kwargs) \
File "/home/sudarshan/python/try/try1.py", line 16, in __init__
self.uid = uid
File "/home/sudarshan/python/try/venv/lib/python3.6/site-packages/cassandra/cqlengine/models.py", line 318, in __set__
return instance._values[self.column.column_name].setval(value)
AttributeError: 'Student' object has no attribute '_values'
127.0.0.1 - - [17/Jan/2020 15:38:34] "GET /add?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [17/Jan/2020 15:38:34] "GET /add?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [17/Jan/2020 15:38:34] "GET /add?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [17/Jan/2020 15:38:34] "GET /add?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -