nameerror:未定义名称“marks”

koaltpgm  于 2021-06-10  发布在  Cassandra
关注(0)|答案(3)|浏览(379)

在这里,我试图用filter()从cassandra获取数据,在这里我需要获取大于或等于65分的学生,但是我得到了这个错误,我不明白为什么我会得到这个错误。我指的是这个链接。我也提到了类似的问题,但没有得到任何解决办法。下面是我的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)
    marks = db.columns.Integer(primary_key=True)
    username = db.columns.Text(required=True)
    password = db.columns.Text()

@app.route('/merit')
    def show_merit_list():
        ob = Student.objects.filter(marks >= 65) 
        return render_template('merit.html', ml = ob)

这是我得到的错误日志:

Traceback (most recent call last)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 2463, in 
__call__
return self.wsgi_app(environ, start_response)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 2449, in 
wsgi_app
response = self.handle_exception(e)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1866, in 
handle_exception
reraise(exc_type, exc_value, tb)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in 
reraise
raise value
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 2446, in 
wsgi_app
response = self.full_dispatch_request()
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1951, in 
full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1820, in 
handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in 
reraise
raise value
File "/home/sudarshan/.local/lib/python3.6/site-packages/flask/app.py", line 1949, in 
full_dispatch_request
rv = self.dispatch_request()
File "/home/sudarshan/.local/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/Downloads/PycharmProjects/try/try1.py", line 67, in show_merit_list
ob = Student.objects.filter(marks >= 65)
NameError: name 'marks' is not defined
qc6wkl3g

qc6wkl3g1#

通过 self 对象,从而允许它访问 marks 数据成员。
改变 marksself.marks .

@app.route('/merit')
    def show_merit_list(self):
        ob = Student.objects.filter(self.marks >= 65) 
        return render_template('merit.html', ml = ob)
s6fujrry

s6fujrry2#

最后我终于找到了答案,我忘记了使用allow\u filtering()。代码如下所示:

@app.route('/merit')
def show_merit_list():
    ob = Student.objects().filter() #all()
    ob = ob.filter(Student.marks >= 65).allow_filtering()
    return render_template('merit.html', ml = ob)
qyuhtwio

qyuhtwio3#

如果需要使用筛选运算符,请尝试:

ob = Student.objects.filter(marks__gte=65)

相关问题