0

I am completely new to the Python world, but I am trying to learn this beautiful language. But I need your help.

I have an API, and I want it when a user wants to change the value of any data about his product, and it is updated with the new value.

For example:

I have the following product:

  • Title = DC snowboard boots
  • Description = Hi everyone, I'm selling this snowboard boots and both are brand new for £ 130
  • Price = £ 130

And then the user makes a request to change for example the price of the product.

And completely lost in how to do that.

My view for update the product:

import app.utils.responses as resp
from app.api import api
from app.database import db
from app.models.products import Products, ProductsSchema
from app.utils.responses import m_return

@api.route('/product/<product_id>', methods=['PATCH'])
def update_product(product_id):
    # Get products data
    product_to_update = Products.query.get(product_id)
    Products.id = product_to_update
    result = Products.id

    db.session.merge(result)
    db.session.commit()

    # Products schema for some fields.
    products_schema = ProductsSchema(
    only=('id', 'title', 'description', 'post_code', 'product_name',     'is_available', 'created', 'price'))

    # Return item updated.
    return m_return(
        http_code=resp.PRODUCT_UPDATED_SUCCESSFULLY['http_code'],
        message=resp.PRODUCT_UPDATED_SUCCESSFULLY['message'],
        value=products_schema.dump(result).data
    )

1 Answers1

0
product_to_update = Products.query.get(product_id)

This code returns a Products object if product_id is in db, if yes then you can update columns of this product:

product_to_update.price = 123#new price
db.session.commit()

So for this specific product the price will be update.

metmirr
  • 4,234
  • 2
  • 21
  • 34