Please help, I'm new on Odoo:
1. I want to get Product's template from selected Product category
2. Get Products name from product template
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project
- MRP
This question has been flagged
Hi,
You can use relational fields for this, ie, many2many or many2one fields. Assuming if you are going with many2one field, define two fields with co model as product and category.
product_categ_id = fields.Many2one('product.category', string="Category")
product_id = fields.Many2one('product.product', string="Product", domain="[('categ_id', '=', product_categ_id)]")
Thanks
Hi
Get the Product's template from the selected Product category
Try the code given below,
def get_product_templates_for_category(selected_category_id):
# Access the 'product.template' model
ProductTemplate = env['product.template']
# Search for Product Templates based on the selected Product Category ID
templates = ProductTemplate.search([('categ_id', '=', selected_category_id)])
# Return the result (list of product.template records)
return templates
# Call the function with the selected Product Category ID
selected_category_id = 1 # Replace with the actual ID of the selected category
templates_for_category = get_product_templates_for_category(selected_category_id)
# Now, `templates_for_category` will contain the list of product.template records associated with the selected category.
To get the Product's name from the product template, try the function given below,
def get_products_names_for_template(product_template_id):
# Access the 'product.product' model
ProductProduct = env['product.product']
# Search for product variants based on the product template ID
products = ProductProduct.search([('product_tmpl_id', '=', product_template_id)])
# Get the names of the products from the product variants
product_names = products.mapped('name')
# Return the result (list of product names)
return product_names
# Call the function with the product template ID for which you want to get the products' names
product_template_id = 1 # Replace with the actual ID of the product template
products_names_for_template = get_products_names_for_template(product_template_id)
# Now, `products_names_for_template` will contain the list of product names associated with the specified product template.
Hope it helps
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up
Thank you so much @Niyas Raphy