I have a Post model as follows -
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
I am unable to update objects using the PUT request. Here is my code -
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)
The Post object is not updated.
Instead of manually updating the post.__dict__
use setattr
.