Answer the question
In order to leave comments, you need to log in
Flask. Why does it throw a TypeError when trying to write data to the database?
Hello.
I'm trying to get to grips with flask and writing a small blog app.
Now I am registering users and when I try to enter data into the database, an error is generated
TypeError: add() missing 1 required positional argument: 'instance'
app/models.py
from werkzeug.security import generate_password_hash, check_password_hash
from app import db
from config import ROLE_USER
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
role = db.Column(db.SmallInteger, default=ROLE_USER.get('user'))
email = db.Column(db.String(120), index=True, unique=True)
pwd_hash = db.Column(db.String(100))
nickname = db.Column(db.String(64), index=True, unique=True)
about_me = db.Column(db.Text)
last_seen = db.Column(db.DateTime)
posts = db.relationship('Post', backref='author', lazy='dynamic')
comments = db.relationship('Comment', backref='author', lazy='dynamic')
@property
def is_authenticated(self):
return True
@property
def is_active(self):
return True
@property
def is_anonymous(self):
return False
@property
def get_id(self):
return str(self.id)
def set_password(self, password):
self.pwd_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwd_hash, password)
def __init__(self, nickname, email, password):
self.nickname = nickname
self.email = email
self.set_password(password)
def __repr__(self):
return '<User %r>' % self.nickname
/app/views.py
from flask import render_template, redirect, url_for, flash, request
from .forms import LoginForm, RegisterForm
from .models import User, Post
from app import app, db
@app.route('/')
def index():
text = 'Home Page'
return render_template('index.html',
text=text)
@app.route('/signup', methods=['GET', 'POST'])
def create_user():
form = RegisterForm()
if form.validate_on_submit():
new_user = User(form.nickname.data, form.email.data, form.password.data)
db.Session.add(new_user)
db.Session.commit()
return redirect(url_for('index'))
return render_template('register.html',
title='Sign Up',
form=form)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question