__init__() got an unexpected keyword argument 'author' for one to many
Question:
So, im trying to reference a users post ? and i keep getting this error
error
>>> from app import db, models
>>> u = models.User.query.get(2)
>>> p = models.Post(title='barn owl', body='thompson', author=u)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'author'
I’m trying to reference posts by users.
Models.py
from app import app, db, bcrypt, slugify, flask_whooshalchemy, JWT, jwt_required, current_identity, safe_str_cmp
from sqlalchemy import Column, Integer, DateTime, func
from app import (TimedJSONWebSignatureSerializer
as Serializer, BadSignature, SignatureExpired)
import datetime
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
password = db.Column(db.String(20), unique=True)
posts = db.relationship('Post', backref='author', lazy='dynamic')
def __init__(self, username, password):
self.username = username
self.password = bcrypt.generate_password_hash(password, 9)
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return (self.id)
def __repr__(self):
return '<User %r>' % self.username
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
# slug = db.Column(db.String(80), index=True, nullable=True)
time_created = Column(DateTime(timezone=True), server_default=func.now())
time_updated = Column(DateTime(timezone=True), onupdate=func.now())
def __init__(self, title, body):
self.title = title
self.body = body
# self.slug = slugify(title).lower()
I’m pretty confused, i have been referencing the flask mega tutorial im not really having success, does anyone have any suggestions, im about to go nuts
Answers:
What you need is:
p = models.Post(title='barn owl', body='thompson', user_id=u.id)
# ^^^^^^^ ^ ^^
And as Kyle mentioned, add the user_id
argument to the __init__
of Post
:
def __init__(self, title, body, user_id):
...
self.body = body
self.user_id = user_id
...
Because your post contains a user_id
, not an author
:
class Post(db.Model):
...
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
...
and your user has an id
:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
...
Or just add author as a relationship and add author as a parameter to the __init__
method
# inside Post definition
author = db.relationship("User")
def __init__(self,title,body,author):
self.author = author
So, im trying to reference a users post ? and i keep getting this error
error
>>> from app import db, models
>>> u = models.User.query.get(2)
>>> p = models.Post(title='barn owl', body='thompson', author=u)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'author'
I’m trying to reference posts by users.
Models.py
from app import app, db, bcrypt, slugify, flask_whooshalchemy, JWT, jwt_required, current_identity, safe_str_cmp
from sqlalchemy import Column, Integer, DateTime, func
from app import (TimedJSONWebSignatureSerializer
as Serializer, BadSignature, SignatureExpired)
import datetime
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
password = db.Column(db.String(20), unique=True)
posts = db.relationship('Post', backref='author', lazy='dynamic')
def __init__(self, username, password):
self.username = username
self.password = bcrypt.generate_password_hash(password, 9)
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return (self.id)
def __repr__(self):
return '<User %r>' % self.username
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
# slug = db.Column(db.String(80), index=True, nullable=True)
time_created = Column(DateTime(timezone=True), server_default=func.now())
time_updated = Column(DateTime(timezone=True), onupdate=func.now())
def __init__(self, title, body):
self.title = title
self.body = body
# self.slug = slugify(title).lower()
I’m pretty confused, i have been referencing the flask mega tutorial im not really having success, does anyone have any suggestions, im about to go nuts
What you need is:
p = models.Post(title='barn owl', body='thompson', user_id=u.id)
# ^^^^^^^ ^ ^^
And as Kyle mentioned, add the user_id
argument to the __init__
of Post
:
def __init__(self, title, body, user_id):
...
self.body = body
self.user_id = user_id
...
Because your post contains a user_id
, not an author
:
class Post(db.Model):
...
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
...
and your user has an id
:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
...
Or just add author as a relationship and add author as a parameter to the __init__
method
# inside Post definition
author = db.relationship("User")
def __init__(self,title,body,author):
self.author = author