콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
143 화면

Hello everyone,

I'm learning Odoo development and I've run into a very strange issue that I haven't been able to solve, even after a clean reinstall of the system. I would greatly appreciate your help and expertise.

Environment:

  • Odoo Version: 19 Community
  • Setup: Official Windows Installer
  • Context: Local development environment on C:\Odoo19 (to avoid permission issues).

Objective: To make the product search on the shop (/shop) more precise by searching only for whole words.

The Problem in Action:

When I search for the term "ram", Odoo's default broad search finds the substring "ram" anywhere. This returns incorrect results like "Heramienta" (Spanish for "tool") and "Programa" (Spanish for "program").

The Log Evidence:

The strangest part is that, at the same time, the server log (odoo.log) proves that my custom controller is executing and that my Python filtering logic is perfect. The list of products my code generates is correct, containing only RAM products.

Summary of the Problem:

In short: the code executes correctly, it filters the product list perfectly (confirmed by the log), but the final webpage ignores this correct list and displays the incorrect one. Even using "request.render()" to force a re-render of the template, the visual result does not change.

My Code "(controllers/ main.py)":


# -*- coding: utf-8 -*-

import logging

import re

from odoo import http

from odoo.http import request

from odoo.addons.website_sale.controllers.main import WebsiteSale


_logger = logging.getLogger(__name__)


class CustomWebsiteSearch(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, sitemap=WebsiteSale.sitemap_shop)

    def shop(self, page=0, category=None, search='', min_price=0.0, max_price=0.0, ppg=False, **post):

        response = super(CustomWebsiteSearch, self).shop(

            page=page, category=category, search=search, min_price=min_price,

            max_price=max_price, ppg=ppg, **post

        )


        if not search or not response.qcontext.get('products'):

            return response


        _logger.info("--- 1. Netdata Controller INITIALIZED for: '%s' ---", search)


        all_products = response.qcontext['products']

        term = search.lower()

        relevant_products = []


        for product in all_products:

            pattern = r'\b' + re.escape(term) + r'\b'

            if re.search(pattern, product.name.lower()):

                relevant_products.append(product)


        _logger.info("--- 2. Products AFTER filter: %s ---", [p.name for p in relevant_products])

       

        qcontext = response.qcontext

        qcontext['products'] = http.request.env['product.template'].browse([p.id for p in relevant_products])

        qcontext['search_count'] = len(relevant_products)


        return request.render('website_sale.products', qcontext)


What could cause Odoo to ignore the "return request.render(...)" and fail to display the updated data from "qcontext", even when the logs prove the logic is correct and executing? Is this a known issue in Odoo 19 Community? Could it be a conflict with the theme or a cache that isn't being cleared properly?

아바타
취소
베스트 답변

There is more to the /shop/... routes than is covered by your source - see also https://github.com/odoo/odoo/blob/19.0/addons/website_sale/controllers/main.py#L487.

What's most crucial to your scenario is the bins key.

A working source - but far from being optimized nor covering all aspects such as filters etc. - is:

# -*- coding: utf-8 -*-

import logging
import re
from odoo.tools import lazy
from odoo import http
from odoo.http import request
from odoo.addons.website_sale.controllers.main import WebsiteSale, TableCompute

_logger = logging.getLogger(__name__)


class CustomWebsiteSearch(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, sitemap=WebsiteSale.sitemap_shop)
    def shop(self, page=0, category=None, search='', min_price=0.0, max_price=0.0, ppg=False, **post):
        response = super(CustomWebsiteSearch, self).shop(
            page=page, category=category, search=search, min_price=min_price,
            max_price=max_price, ppg=ppg, **post
        )

        website = request.env['website'].get_current_website()
        ppr = website.shop_ppr or 4

        if not search or not response.qcontext.get('products'):
            return response

        _logger.info("--- 1. Netdata Controller INITIALIZED for: '%s' ---", search)
        all_products = response.qcontext['products']
        term = search.lower()
        relevant_products = []
        for product in all_products:
            pattern = r'\b' + re.escape(term) + r'\b'
            if re.search(pattern, product.name.lower()):
                relevant_products.append(product)

        _logger.info("--- 2. Products AFTER filter: %s ---", [p.name for p in relevant_products])
        qcontext = response.qcontext
        qcontext['products'] = http.request.env['product.template'].browse([p.id for p in relevant_products])
        qcontext['search_product'] = http.request.env['product.template'].browse([p.id for p in relevant_products])
        qcontext['bins'] = lazy(lambda: TableCompute().process(relevant_products, ppg, ppr))
        qcontext['search_count'] = len(relevant_products)
        return request.render('website_sale.products', qcontext)
아바타
취소
관련 게시물 답글 화면 활동
3
9월 25
654
1
9월 25
2025
1
8월 25
499
0
7월 25
1266
1
5월 25
1990