所以,即時通訊嘗試引用用戶的職位?我不斷收到此錯誤__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我不是真的有成功的,沒有任何人有任何建議,我將要發瘋
刪除''中P = models.Post author'參數(標題=「穀倉貓頭鷹,身體= '湯普森',作者= U)' –
這樣的作品,但它不引用網友的帖子,你知道我的意思 ? – BARNOWL