2015-12-26 61 views
0

我有一個Post模型如下 -瓶的RESTful API把錯誤

class Post(db.Model): 
    id = db.Column(db.Integer, primary_key=True, autoincrement=True) 
    likes = db.Column(db.Integer) 
    body = db.Column(db.Text) 
    user_id = db.Column(db.Integer, db.ForeignKey('user.id')) 

    def __init__(self, body, user_id): 
     self.likes = 0 
     self.body = body 
     self.user_id = user_id 

我無法更新使用PUT請求的對象。這裏是我的代碼 -

class UserPost(Resource): 
    def get(self, post_id): 
     print(post_id) 
     return marshal(Post.query.get(post_id), post_fields) 

    def put(self, post_id): 
     post = Post.query.get_or_404(post_id) 
     args = post_parser.parse_args() 
     print args 
     for k, v in args.iteritems(): 
      if v is not None: 
       post.__dict__[k] = v 

       db.session.add(post) 
       db.session.commit() 
     return marshal(post, post_fields) 

Post對象沒有更新。

+0

什麼是錯誤? – mic4ael

+0

您是否嘗試過:'@ app.route('/ path',methods = ['PUT','POST'])' – Valijon

+0

沒有錯誤,對象不會更新並返回舊的。 – utkbansal

回答

1

而不是手動更新post.__dict__使用setattr