2016-11-22 100 views
1

所以,即時通訊嘗試引用用戶的職位?我不斷收到此錯誤__init __()得到一個意想不到的關鍵字參數'作者'一對多

錯誤

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' 

我試圖通過用戶參考文章。

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我不是真的有成功的,沒有任何人有任何建議,我將要發瘋

+0

刪除''中P = models.Post author'參數(標題=「穀倉貓頭鷹,身體= '湯普森',作者= U)' –

+1

這樣的作品,但它不引用網友的帖子,你知道我的意思 ? – BARNOWL

回答

2

或者只是添加筆者作爲一名關係並將作者作爲參數添加到方法

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

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

讓我給這個鏡頭凱爾 – BARNOWL

+0

它給了我這個 sqlalchemy.exc.ArgumentError:創建關係'User.posts'的backref'作者'時出錯:該名稱的屬性存在於映射器'Mapper | Post | posts' – BARNOWL

+0

從User類刪除帖子行,並繼續使用User.posts,只需在作者關係中添加backref =「posts」 –

2

什麼你需要的是:

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

而且,正如凱爾提到的,將user_id參數添加到__init__Post的:

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

因爲你的帖子包含一個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) 
    ... 
+0

它沒有工作,我不斷得到__init __()有一個意外的關鍵字參數 – BARNOWL

+0

你現在得到的完全錯誤是什麼? – Bahrom

+1

這是因爲他正在定義'__init__',並且沒有告訴它期望一個名爲author的參數,它非常清楚 –

相關問題