How to show menu badge in odoo

In odoo you can add easily menu badges on menu items. Odoo provides a best and very easy way to do that. You can add numeric values next to the menu items through few lines of code.



How to show menu badge in odoo

What are menu badges in odoo?


Menu badges are nothing just numeric values (1,2,3..) next to the menu items. The question is why we need these menu badges. The answer is we want to show the user that, following number of task(s)/work has been pending in the following "state" or "workflow".

Adding menu badges in odoo


You do not need to do anything more than what's shown in the post. You just need toiInheriting from the "mixin" and "defining the method" named "_needaction_domain_get" is all that you need. Odoo will automatically pick it up and display next to all menu items pointing to actions for this particular model. Below are the code snippet, that you just need to copy and paste.


class your_model(models.Model):
_name = 'your.model'
_inherit = ['ir.needaction_mixin']
state=fields.Selection([('Draft','Draft'),('Submitted','Submitted'),('Approved','Approved')],'State',default='Draft')

@api.model
def _needaction_domain_get(self):
# getting login user groups
user_groups = self.env['get.user.groups'].get_groups_function(self.env.user.login)

# Check if "your_desired_group" is in above groups than show numeric values on the menu, for the state "Submitted"
if set(['your_desired_group']) & set(user_groups):
return [('state', '=', 'Submitted')]
else:
return False


Code Description

To use badges in our menu we just need to inherit "ir.needaction_mixin". After inheriting, just define a method named "_needaction_domain_get" this method will return a domain that selects these states value that should be counted towards the number on the badge. In our case it would be "Submitted".