0

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.

teddcp
  • 1,514
  • 2
  • 11
  • 25
  • 1
    Neither of those links show that you can use the object as a dictionary, but just as an object. `bucket_to_update.name = name` would match the syntax in your linked answer, _not_ `bucket_to_update['name'] = name`. There is also (usually) no need to explicitly commit with Flask-SqlAlchemy, since it'll do that as long as the returned status is not a 4xx or 5xx error. – MatsLindh Jan 04 '21 at 10:15
  • @MatsLindh, i was blinded by that dictionary thing and half of time i was worried! Thanks alot – teddcp Jan 04 '21 at 10:34

2 Answers2

2

I don't have access to my coding environment but I reckon if you do following it should work fine.

    bucket_to_update.name = name
Divakar Patil
  • 323
  • 2
  • 11
0

Adding (3) at the end of def put(self, id):, Correct return {"status" "success"}, 200 to return {"status":"success"}, 200

(1) In your class Bucket(db.Model): you define the initialization function __init__ so you override that of the parent class db.Model, so the __init__ function of the parent class will not be invoked.

Therefore, try:

    def __init__(self, name):
        db.Model.__init__(self) ## *** invoke __init__ of the parent class
        self.name = name

(2) to update the modification in bucket_to_update in:

        bucket_to_update['name'] = name  # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
        db.session.commit()

add db.session.add(bucket_to_update) before commit:

        bucket_to_update['name'] = name  # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
        db.session.add(bucket_to_update)
        db.session.commit()

looking for your comments.

Good Luck.

Nour-Allah Hussein
  • 1,439
  • 1
  • 8
  • 17