norsemanGrey
norsemanGrey

Reputation: 355

Get TypeError "Unexpected Keyword Argument" on Query.Get

I am following this Flask Mega-Tutorial and have run into an issue some way in. I am using PyCharm CE for Windows.

The part where I am running into an issue is after adding a user registration form to the application. When running the app and trying to register a new user in the browser I get the following error from the form validation:

File "C:\Users\janmar\OneDrive - KONGSBERG MARITIME AS\Programming\Python\Projects\colligero\app\forms.py", line 49, in validate_username
user = User.query.get(username=username.data).first()
TypeError: get() got an unexpected keyword argument 'username'

Which comes from a custom validation method 'def validate_username' in the forms.py module. I guess it is not able to "find" the 'username' item in the User class in the models.py module?

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, ValidationError, Email, EqualTo, Regexp, Length
from app.models import User


class LoginForm(FlaskForm):

    username = StringField("Username", validators=[DataRequired()])
    password = PasswordField("Password", validators=[DataRequired()])
    remember_me = BooleanField("Remember Me")
    submit = SubmitField("Sign In")


class RegistrationForm(FlaskForm):

    first_name = StringField("First Name", validators=[
        Regexp(regex=r'^[a-zA-Z]+$', message="First name can not contain numbers or special characters.")])
    last_name = StringField("Last Name", validators=[
        Regexp(regex=r'^[a-zA-Z]+$', message="First name can not contain numbers or special characters.")])
    username = StringField("Username", validators=[
        Regexp(regex=r'^\w+$', message="Username can only contain letters, numbers or underscore."),
        Length(min=5, max=25, message="Username must be between 5 and 25 characters.")])
    email = StringField("Email", validators=[
        DataRequired(),
        Email()])
    password = PasswordField("Password", validators=[
        DataRequired()])
    confirm = PasswordField("Repeat Password", validators=[
        DataRequired(), EqualTo('password')])
    submit = SubmitField("Register")

    def validate_username(self, username):

        user = User.query.get(username=username.data).first()

        if user is not None:

            raise ValidationError("Username is already taken by another. Please select a different username.")

    def validate_email(self, email):

        user = User.query.get(email=email.data).first()

        if user is not None:

            raise ValidationError("Email is already taken by another user. Please select a different email.")

This is the content of the models.py file.

from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from app import db, login


class User(UserMixin, db.Model):

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(32), index=True)
    last_name = db.Column(db.String(32), index=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Post', backref='author', lazy='dynamic')

    def set_password(self, password):

        self.password_hash = generate_password_hash(password)

    def check_password(self, password):

        return check_password_hash(self.password_hash, password)

    def __repr__(self):

        return "<User {}>".format(self.username)


@login.user_loader
def load_user(id):

    return User.query.get(int(id))

I have tried querying the the User table the same way from a terminal session an it seems to work fine so I do not know what might be causing the issue here. Also, the PyCharm editor seems to raise a warning on the the two validation methods 'validate_username' and 'validate_email' saying the the methods "may be 'static'". Can this have anything to do with the error I am getting?

Upvotes: 0

Views: 4492

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

I think you are confusing the syntax with Django's here. In Flask-SQLAlchemy, get doesn't take keyword arguments - it is for queries via PK only, as you correctly do in load_user. You need to use filter.

user = User.query.filter(username=username.data).first()

Upvotes: 2

Related Questions