Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
5296 Weergaven

Hello,


I'm using the xmlrpc api to update some information on a product.

The "description" field does not seem to be updated and I believe that this behaviour is due to some cache not being flushed.

I have read about the "invalidate_all" method but I don't know how to use it.

Could you help please ?

Hereafter the update code:

productid = models.execute_kw("$db", uid, "$password", 'product.template', 'search', [[['default_code', "=", sku]]])

print(productid)

result = models.execute_kw("$db", uid, "$password", 'product.template', 'write', [productid, {

    'name': name,

    'list_price': pv,

    'standard_price': pa,

    'description': description,

    'categ_id': categ,

    'detailed_type': producttype,

    'sale_ok': sale_ok,

    'purchase_ok': purchase_ok,

    }])



Avatar
Annuleer
Auteur Beste antwoord


Thanks a lot for this answer.

However I get these error messages:

'ir.model.data' has no attribute 'xmlid_to_res_id'

'product.template' has no attribute 'invalidate_all'


Could anyone confirm that these attributes are still supported in Odoo 16 ?




Avatar
Annuleer
Beste antwoord

try this way:
# Import the xmlrpc.client library

import xmlrpc.client


# Define your Odoo server information

url = 'http://your_odoo_server_url/xmlrpc/2/common'

db = 'your_database_name'

username = 'your_username'

password = 'your_password'


# Create a connection to the Odoo server

common = xmlrpc.client.ServerProxy(url)


# Authenticate and get the user ID (uid)

uid = common.authenticate(db, username, password, {})


# Check if the authentication was successful

if uid:

    # Define your product information

    sku = 'your_product_sku'

    name = 'New Product Name'

    pv = 100.0

    pa = 80.0

    description = 'New Product Description'

    categ = category_id  # Replace with the actual category ID

    producttype = 'your_product_type'

    sale_ok = True

    purchase_ok = True


    # Search for the product ID

    models = xmlrpc.client.ServerProxy('http://your_odoo_server_url/xmlrpc/2/object')

    product_id = models.execute_kw(db, uid, password, 'product.template', 'search', [[['default_code', "=", sku]]])


    if product_id:

        # Update the product information

        result = models.execute_kw(db, uid, password, 'product.template', 'write', [[product_id], {

            'name': name,

            'list_price': pv,

            'standard_price': pa,

            'description': description,

            'categ_id': categ,

            'detailed_type': producttype,

            'sale_ok': sale_ok,

            'purchase_ok': purchase_ok,

        }])


        if result:

            # Flush the cache to ensure the changes are immediately visible

            models.execute_kw(db, uid, password, 'ir.model.data', 'xmlid_to_res_id', ['product.template'], {})

            models.execute_kw(db, uid, password, 'product.template', 'invalidate_all', [[]])

            print("Product information updated successfully.")

        else:

            print("Failed to update product information.")

    else:

        print("Product not found.")

else:

    print("Authentication failed.")


Avatar
Annuleer