gpt4 book ai didi

python - 属性错误 : 'Student' object has no attribute '_values'

转载 作者:行者123 更新时间:2023-12-04 10:44:18 25 4
gpt4 key购买 nike

我正在尝试使用 cassandra 并使用 Flask-CQLAlchemy 库编写 flask 应用程序。当我尝试创建我的模型类(学生)的对象时,它给了我一个名为 的错误。 'Student' 对象没有属性 '_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 -

最佳答案

您没有使用 CQLAlchemy图书馆正确。

您不需要构造函数,但需要使用 create功能。只要你的课就足够了。此外,您的代码中存在语法错误:

student1 = Student(request.form['uid'], request.form['username'], request.form['password'])
student.save()

应该是 student1.save()而不是 student.save() .

您的模型定义将如下所示:
class Student(db.Model):
uid = db.columns.Integer(primary_key=True)
username = db.columns.Text(required=True)
password = db.columns.Text()

在数据库中创建一个条目:
student = Student.create(uid=request.form['uid'], username=request.form['username'], password=request.form['password'])
student.save()

您可以在模型文档 here. 中找到更多相关信息。

关于python - 属性错误 : 'Student' object has no attribute '_values' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com