In Odoo, a qcontext variable is a dictionary that allows you to pass additional context data to the template from a controller method. The acronym stands for "quick context" and is used to provide specific data or variables to the template rendering process.
The qcontext variable can be used to define variables that are not part of the default context, allowing you to customize the behavior or appearance of the template. It is commonly used to pass additional information to a template that derives from a controller method.
Controllers acting as frontend modules integrate seamlessly with backend modules to facilitate the rendering of requests to the web browser.
Overriding a controller involves using a property to introduce modifications to the base class. In the process of rewriting a controller, the first step is to import the base class into Python's controller.py file. Modifications can then be made within the subclass to meet specific requirements.
The use of content or q context finds application mainly in the class of web sales. Product values are rendered within the website base class. These values are passed through a controller that is accessible as a context in the child class that is being overridden.
The controller supplies values and enables their use and possible modifications.
Within the website_sale module, the Website Sale Base class takes care of rendering the store controller and displaying the products and details of the store.
Here, it shows what it returns.
{
'search': fuzzy_search_term or search,
'original_search': fuzzy_search_term and search,
'order': post.get('order', ''),
'category': category,
'attrib_values': attrib_values,
'attrib_set': attrib_set,
'pager': pager,
'pricelist': pricelist,
'fiscal_position': fiscal_position_sudo,
'add_qty': add_qty,
'products': products,
'search_product': search_product,
'search_count': product_count, # common for all searchbox
'bins': lazy(lambda: TableCompute().process(products, ppg, ppr)),
'ppg': ppg,
'ppr': ppr,
'categories': categs,
'attributes': attributes,
'keep': keep,
'search_categories_ids': search_categories.ids,
'layout_mode': layout_mode,
'products_prices': products_prices,
'get_product_prices': lambda product: lazy(lambda: products_prices[product.id]),
'float_round': tools.float_round,
}
Here, it returns the product template and values.
when we rewrite the controller for our development, we can modify its behavior and add updates to the values it uses. These updated values can then be accessed within the controller code using the "q context"
Here, illustrating an example of customization on the shop page.
Website sale Controller can override as follows
from odoo.addons.website_sale.controllers.main import WebsiteSale
Here, the WebsiteSale class is inherited by calling the same shop function within that class and making the required changes.
from odoo import http
from odoo.addons.website_sale.controllers.main import WebsiteSale
class WebsiteSaleInherit(WebsiteSale):
@http.route([
'/shop',
'/shop/page/<int:page>',
'/shop/category/<model("product.public.category"):category>',
'/shop/category/<model("product.public.category"):category>/page/<int:page>',
], type='http', auth="public", website=True)
def shop(self, page=0, category=None, search='', min_price=0.0,
max_price=0.0, ppg=False, **post):
""" Returns the categories for the shop specifically that are chosen in the menu"""
res = super(WebsiteSaleInherit, self).shop(page, category, search,
min_price, max_price, ppg,
**post)
products = res.qcontext.get('products')
for product in products:
if product.id == 16:
product.name = 'Session Chair'
return res
Qcontext allows us to send the changed product name to the template. It lets us include dynamic information in templates, making it easy to customize content. Once we add the modified product name to the qcontext, it can be shown in the template alongside the product display.
Here, we updated the product name from "Conference Chair" to "Session Chair" after retrieving the product details using the qcontext parameter.
In conclusion, using the qcontext variable lets you add extra information to templates from controller methods. This approach is particularly useful for supplementing templates with extra information originating from controller methods. Essentially, the qcontext variable serves as a bridge between the backend logic (handled by controller methods) and the frontend presentation (handled by templates), allowing for smooth communication and customization between the two.
To read more about Q context works in Odoo 16, refer to our blog How Q Context Works In Odoo 16