pythonflaskcassandracqlcqlengine

AttributeError: 'Student' object has no attribute '_values'


I am trying to write a flask application with cassandra and using Flask-CQLAlchemy library. And when I am trying to create an object of my model class(Student) it's giving me an error called 'Student' object has no attribute '_values'. Went through multiple solutions matching my error but not able to solve this.

Following is the file containing python code:

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)

This one is the index.html file

<!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>

Error log:

 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 -

Solution

  • You are not using the CQLAlchemy library properly.

    You don't need a constructor but need to use the create function. Just your class will suffice. Also, there is a syntax error in your code:

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

    it should be student1.save() and not student.save().

    Your model definition will look something like this:

    class Student(db.Model):
        uid = db.columns.Integer(primary_key=True)
        username = db.columns.Text(required=True)
        password = db.columns.Text()
    

    Create an entry in the DB with:

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

    You can find more about it in the documentation for models here.