__init__() 得到了一个意外的一对多关键字参数 'author'

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

所以,我想引用一个用户 post?我一直收到这个错误

错误

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'

我正在尝试让用户引用 posts。

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()

我很困惑,我一直在参考 flask mega tutorial 我并没有真正成功,有没有人有任何建议,我快要疯了

您需要的是:

p = models.Post(title='barn owl', body='thompson', user_id=u.id)
#                                                  ^^^^^^^ ^ ^^

正如 Kyle 提到的,将 user_id 参数添加到 Post__init__ 中:

def __init__(self, title, body, user_id):
    ...
    self.body = body
    self.user_id = user_id
    ...

因为您的 post 包含 user_id,而不是 author:

class Post(db.Model):
    ...
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    ...

并且您的用户有 id:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ...

或者只添加作者作为关系并将作者作为参数添加到 __init__ 方法

# inside Post definition
author = db.relationship("User")

def __init__(self,title,body,author):
    self.author = author