Owl Man Owl Man - 15 days ago 4
Python Question

__init__() got an unexpected keyword argument 'author' for one to many

So, im trying to reference a users post ? and i keep getting this error


Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db, models
/usr/local/lib/python2.7/dist-packages/Flask-0.11.1-py2.7.egg/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead.
.format(x=modname), ExtDeprecationWarning
>>> 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.


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


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