【问题标题】:NameError: name 'marks' is not definedNameError:名称“标记”未定义
【发布时间】:2020-05-16 21:59:16
【问题描述】:

在这里,我尝试使用 filter() 从 cassandra 获取数据,我需要获取大于或等于 65 分的学生,但我收到此错误无法理解为什么会收到此错误。我指的是this 链接。我也提到了类似的问题,但没有得到任何解决方案。 这是我的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

【问题讨论】:

  • 在您的过滤器中尝试 self.marks

标签: python flask cassandra cql


【解决方案1】:

self 对象传递给您的方法,从而允许它访问marks 数据成员。

marks 更改为self.marks

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

【讨论】:

    【解决方案2】:

    最后我找到了我忘记使用 allow_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)
    

    【讨论】:

      【解决方案3】:

      你需要使用Filtering Operators,试试:

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

      【讨论】:

      • 但是当我删除 > 符号并只保留 = 符号正在工作并显示所有学生 @explodinggayfish
      • 在传递参数时不能使用>符号,如果是这样,marks >= 65参数将被执行并转换为bool变量,这将导致错误,因为现在mark不是参数名称不再是一个变量。你可以试试x = 5;print(x > 3)
      • 在这种情况下,我认为您的数据或查询有问题
      • 跟allow_filtering()有什么关系吗?
      • 'cassandra.cqlengine.query.QueryException:Where 子句需要 =、IN 或 CONTAINS(集合)与主键或索引字段进行比较。您可能需要考虑在您管理 cqlengine 之外的索引的字段上设置 custom_index。是我喜欢这个'ob = Student.objects.filter(Student.marks>=65)'时遇到的错误
      猜你喜欢
      • 2020-06-27
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多