如何在 [odoo12] 中创建网站搜索
How to create a WEBSITE SEARCH in [odoo12]
我想在 odoo 的网站中创建一个搜索字段,以允许我搜索客户的姓名,但我不知道如何添加功能来执行此操作。
能否请您指导我学习可以帮助我创建搜索功能的教程。
我检查了模块 website_sale,我尝试做同样的事情,但它并没有真正起作用。
这是我输入的代码:
class Controllers(http.Controller):
@api.multi
def sale_product_domain(self):
return self.get_current_website().website_domain()
def _get_search_domain(self, search, attrib_values):
domain = Controllers.sale_product_domain(self)
if search:
for srch in search.split(" "):
domain += [
'|', '|', '|', ('name', 'ilike', srch), ('description', 'ilike', srch)]
if attrib_values:
attrib = None
ids = []
for value in attrib_values:
if not attrib:
attrib = value[0]
ids.append(value[1])
elif value[0] == attrib:
ids.append(value[1])
else:
domain += [('attribute_line_ids.value_ids', 'in', ids)]
attrib = value[0]
ids = [value[1]]
if attrib:
domain += [('attribute_line_ids.value_ids', 'in', ids)]
return domain
@http.route('/tabs', type='http', auth='public', website=True)
def navigate_to_tabs(self, search='', ppg=False, **post):
clients = http.request.env['res.partner'].sudo().search([])
if ppg:
try:
ppg = int(ppg)
except ValueError:
ppg = PPG
post["ppg"] = ppg
else:
ppg = PPG
attrib_list = request.httprequest.args.getlist('attrib')
attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v]
attributes_ids = {v[0] for v in attrib_values}
attrib_set = {v[1] for v in attrib_values}
domain = self._get_search_domain(search, attrib_values)
keep = QueryURL('/tabs', search=search, attrib=attrib_list,
)
url = "/tabs"
if search:
post["search"] = search
if attrib_list:
post['attrib'] = attrib_list
Partner = request.env['res.partner']
Category = request.env['product.public.category']
search_categories = False
if search:
categories = Partner.search(domain).mapped('public_categ_ids')
search_categories = Category.search(
[('id', 'parent_of', categories.ids)] + request.website.website_domain())
categs = search_categories.filtered(lambda c: not c.parent_id)
else:
categs = Category.search([('parent_id', '=', False)] + request.website.website_domain())
parent_category_ids = []
values = {
'search': search,
#'category': category,
'attrib_values': attrib_values,
'attrib_set': attrib_set,
# 'search_count': product_count, # common for all searchbox
'rows': PPR,
'categories': categs,
'keep': keep,
'parent_category_ids': parent_category_ids,
'search_categories_ids': search_categories and search_categories.ids,
}
return http.request.render('site_web.s_tabs', {'clients' : clients})
我建议查看此项目以供 website_search 参考:
https://github.com/OpenSur/Odoo_addons/tree/master/website_search
因为它旨在为页面、博客 post、博客 post 评论、客户、工作机会和产品提供一体化搜索界面。我认为它为根据您的特定要求进行定制提供了坚实的基础。你可以参考这个模块。
希望对您有所帮助。谢谢。
我想在 odoo 的网站中创建一个搜索字段,以允许我搜索客户的姓名,但我不知道如何添加功能来执行此操作。 能否请您指导我学习可以帮助我创建搜索功能的教程。
我检查了模块 website_sale,我尝试做同样的事情,但它并没有真正起作用。
这是我输入的代码:
class Controllers(http.Controller):
@api.multi
def sale_product_domain(self):
return self.get_current_website().website_domain()
def _get_search_domain(self, search, attrib_values):
domain = Controllers.sale_product_domain(self)
if search:
for srch in search.split(" "):
domain += [
'|', '|', '|', ('name', 'ilike', srch), ('description', 'ilike', srch)]
if attrib_values:
attrib = None
ids = []
for value in attrib_values:
if not attrib:
attrib = value[0]
ids.append(value[1])
elif value[0] == attrib:
ids.append(value[1])
else:
domain += [('attribute_line_ids.value_ids', 'in', ids)]
attrib = value[0]
ids = [value[1]]
if attrib:
domain += [('attribute_line_ids.value_ids', 'in', ids)]
return domain
@http.route('/tabs', type='http', auth='public', website=True)
def navigate_to_tabs(self, search='', ppg=False, **post):
clients = http.request.env['res.partner'].sudo().search([])
if ppg:
try:
ppg = int(ppg)
except ValueError:
ppg = PPG
post["ppg"] = ppg
else:
ppg = PPG
attrib_list = request.httprequest.args.getlist('attrib')
attrib_values = [[int(x) for x in v.split("-")] for v in attrib_list if v]
attributes_ids = {v[0] for v in attrib_values}
attrib_set = {v[1] for v in attrib_values}
domain = self._get_search_domain(search, attrib_values)
keep = QueryURL('/tabs', search=search, attrib=attrib_list,
)
url = "/tabs"
if search:
post["search"] = search
if attrib_list:
post['attrib'] = attrib_list
Partner = request.env['res.partner']
Category = request.env['product.public.category']
search_categories = False
if search:
categories = Partner.search(domain).mapped('public_categ_ids')
search_categories = Category.search(
[('id', 'parent_of', categories.ids)] + request.website.website_domain())
categs = search_categories.filtered(lambda c: not c.parent_id)
else:
categs = Category.search([('parent_id', '=', False)] + request.website.website_domain())
parent_category_ids = []
values = {
'search': search,
#'category': category,
'attrib_values': attrib_values,
'attrib_set': attrib_set,
# 'search_count': product_count, # common for all searchbox
'rows': PPR,
'categories': categs,
'keep': keep,
'parent_category_ids': parent_category_ids,
'search_categories_ids': search_categories and search_categories.ids,
}
return http.request.render('site_web.s_tabs', {'clients' : clients})
我建议查看此项目以供 website_search 参考:
https://github.com/OpenSur/Odoo_addons/tree/master/website_search
因为它旨在为页面、博客 post、博客 post 评论、客户、工作机会和产品提供一体化搜索界面。我认为它为根据您的特定要求进行定制提供了坚实的基础。你可以参考这个模块。
希望对您有所帮助。谢谢。