Skip to Content
Menu
This question has been flagged
2 Replies
3022 Views

I've added a searchpanel to the product views to show the categories. But the name of the categories is shown with all the path 




I tried to add something like this to the field:

context="{'show_short_category': True}"


But the context is not passed to the name_get function.


Is there a way to show only the name of the category? (keeping the current display_name for all the other cases (like in the list view)





Avatar
Discard
Best Answer

Hi,

First you have to inherit name_get function and make it run with context variable 'show_category_path'. In this function set '_rec_name' to 'name'.

class ProductCategory(models.Model):
_inherit = "product.category"
_rec_name = 'name'

@api.depends('name')
def name_get(self):

if self\._context\.get\('show_category_path'\):
\ \ \ \ \ \ \ \ \ \ \ \ new_res\ =\ \[\]
\ \ \ \ \ \ \ \ \ \ \ \ for\ category\ in\ self:
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ name\ =\ category\.complete_name
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ new_res\.append\(\(category\.id,\ name\)\)
\ \ \ \ \ \ \ \ \ \ \ \ ctx\ =\ dict\(self\.env\.context,\ show_category_path=False\)
\ \ \ \ \ \ \ \ \ \ \ \ self\.env\.context\ =\ ctx
\ \ \ \ \ \ \ \ \ \ \ \ return\ new_res
\ \ \ \ \ \ \ \ else:
\ \ \ \ \ \ \ \ \ \ \ \ return\ super\(ProductCategory,\ self\)\.name_get\(\)

Second,\ create\ a\ view\ that\ contains\ action\ and\ view\ definitions\ of\ the\ module\ you\ are\ working\ with\ like\ below\. 

The\ first\ context\ change\ is\ in\ action\ definition\ but\ it\ does\ not\ effect\ searchpanel\.\ Because\ of\ this,\ setting\ '_rec_name'\ to\ 'name'\ makes\ it\ work\ as\ you\ need\.\ After\ view\ s\ created\ context\ is\ changed\ with\ your\ variable\ 'show_category_path'\ \.

You\ can\ change\ field\ context\ values\ as\ you\ need\ for\ other\ field\ definitions\ as\ below\.


\ \
\ \ \ \
\ \ \ \ \ \ \ \ Products
\ \ \ \ \ \ \ \ ir\.actions\.act_window
\ \ \ \ \ \ \ \ product\.template
\ \ \ \ \ \ \ \ kanban,tree,form
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \{"search_default_consumable":\ 1,\ 'default_type':\ \ ​\ \ \ \ \ \ 'product','show_category_path':\ True\}
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \


\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Create\ a\ new\ product
\ \ \ \ \ \ \ \ \ \ \ \


\ \ \ \ \ \ \ \
\ \ \ \
\ \ \ \ ir\.ui\.view"\ id="product_template_form_view_sale_order_button">
\ \ \ \ \ \ \ \ product\.template\.sale\.order\.button
\ \ \ \ \ \ \ \ product\.template
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{'show_category_path':\ True\}
\ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \
\ \ \ \
\ \ \ \ \ \
\ \ \ \ "\ rel="ugc">ir\.ui\.view">
\ \ \ \ \ \ \ \ app\.product\.template\.search
\ \ \ \ \ \ \ \ product\.template
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \
\ \ \ \
\ \ \ \
\ \ \ \ "\ rel="ugc">ir\.ui\.view">
\ \ \ \ \ \ \ \ app.stock.quant.template.search
stock.quant













Avatar
Discard
Best Answer

Hi,

Set the context in XML file in the model product

eg:

https://ibb.co/jHTVt2C

In the product category under names get you will get the context while selecting the filed in product.

@api.depends('name', 'code')
def name_get(self):
if not self._context.get('for_partner_id', False):
return super(ResPartner, self).name_get()
else:
res = []
for partner in self:
res.append((partner.id, partner.name))
return res

Regards

Avatar
Discard
Related Posts Replies Views Activity
0
Jul 21
1155
0
Jan 23
809
0
Oct 21
1379
1
Jun 21
1368
2
Feb 21
5118