i am doubtful about my code. After looking for hours, was still not able to figure it out.
it's a very simple thing where i am trying to query a table with a given id. Then i want to update the 'name' attribute with the passed name.
But it's giving me an error- TypeError: 'Bucket' object does not support item assignment
. it's like its not returning a dictionary
# PUT REQUEST
def put(self, id):
bucket_to_update = bucket_db.query.get(id) #### returns <Bucket id> , not dict
print(bucket_to_update) # prints the bucket as string
if not bucket_to_update:
return {"status": "failure"}, 404
args = BucketAPI.parser.parse_args()
name = args.get('name', None)
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.commit()
return {"status" "success"}, 200
Model - Bucket / bucket_db
"""Bucket model"""
from todo_app import db
class Bucket(db.Model):
__tablename__ = 'buckets'
def __init__(self, name):
self.name = name
id = db.Column(
db.Integer,
primary_key=True
)
name = db.Column(
db.Text,
index=True,
unique=True,
nullable=False
)
items = db.relationship('Item',
backref=db.backref('bucket', lazy='joined'),
lazy=True)
def __repr__(self):
return '<bucket-{}> '.format(self.name)
From the docs and link, it is clearly visible that we can update the details as dictionary
, but here its not working.
Erorr logs
bucket_to_update['name'] = name
TypeError: 'Bucket' object does not support item assignment
p.s- i am creating a Resource class to have different creation/deletion methods using flask-restful
UPDATE (solved)
As pointed on comments and answers, its an object than a dictionary, you should access it as bucket.name=name
. Plus I had missed a colon while returning.