Flask: TypeError: blog() got an unexpected keyword argument 'user'

Viewed 1840

I have a model "blog" and I am trying to create an Instance of it and add it to the database. However, when I try to create the instance I get the error TypeError: blog() got an unexpected keyword argument 'user', and when I delete the parameter user it says: TypeError: blog() got an unexpected keyword argument 'title' and so on for all the parameters.
Code:

Model:

class blog(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    user = db.Column("user", db.String)
    title = db.Column("title", db.String())
    body = db.Column("body", db.String())
    images = db.Column("images", ScalarListType())
    def __init__(self, user, title, body, images):
        self.user = user
        self.title = title
        self.body = body
        self.images = images

function:

@app.route('/add_blog', defaults={'realfiles': ""}, methods=['GET', 'POST'])
@app.route('/add_blog:<realfiles>', methods=['GET', 'POST'])
def add_blog(realfiles):
    if request.method == "POST":
        title = request.form["title"]
        body = request.form["body"]
        images = realfiles
        user = '1'
        new_blog = blog(user = user, title = title, body = body, images = images)
        for f in realfiles.split(','):
            os.rename(os.path.join(app.root_path, 'static\\temp', f), os.path.join(app.root_path,'static\\uploads', f))
        db.session.add(new_blog)
        db.session.commit()
        flash(f"added the blog {new_blog.title}!")
        return redirect(url_for("home"))
    if realfiles == "":
        return render_template("add_blog.html")
    return render_template("add_blog.html", realfiles=realfiles.split(','))

the parameter realfiles is a string.

EDIT:

I tried to change the names of the properties, to restart computer, delete database and create a new one, none of the above worked

EDIT 1: I changed the variable to "name" instead of "user". Still not working. Here is the full traceback

TypeError
TypeError: blog() got an unexpected keyword argument 'name'

Traceback (most recent call last)
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\user\.virtualenvs\JoelBlog-UELb-k-q\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\Google Drive\PythonProjects\JoelBlog\app.py", line 96, in add_blog
new_blog = blog(name = user, title = title, body = body, images = images)
TypeError: blog() got an unexpected keyword argument 'name'

Can you help? Thanks

1 Answers

The reason it is failing because you have a init function which does not expect key/value arguments. If there wasn't an init then you could call it as you are trying. Based on how you're calling you would use def init(self, **kwargs), then kwargs.get(...).

class blog(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    user = db.Column("user", db.String)
    title = db.Column("title", db.String())
    body = db.Column("body", db.String())
    images = db.Column("images", ScalarListType())
    def __init__(self, **kwargs):
        self.user = kwargs.get('user')
        self.title = kwargs.get('title')
        self.body = kwargs.get('body')
        self.images = kwargs.get('images')
Related