Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How can i count records in domain and display it in line with menu ?

Dr Obx
on 8/24/15, 5:41 AM 798 views

As I mentioned in title, need count for example all records with state = "in" and add this figure into menu line as it is in Messaging "Inbox"



| 4 3 5
Hyderabad, India

On 8/24/15, 5:58 AM

Dr Obx,

inherit "ir.needaction_mixin" in your class and override _needaction_domain_get()

def _needaction_domain_get(self):
return [('state', '=', 'in')]

 Hope it helps

Hi Pawan, Why you not responding my questions on Skype :):):) Thanx, I'll try (Student Rob)

Dr Obx
on 8/24/15, 8:06 AM

TypeError: _needaction_domain_get() takes exactly 1 argument (4 given)

Dr Obx
on 8/24/15, 8:11 AM

add @api.model decorator to _needaction_domain_get function definition. as follows:

def _needaction_domain_get(self):
    return [('state', '=', 'in')]
on 8/24/15, 9:15 AM

or change it to v7 style:

def _needaction_domain_get(self, cr, uid, context=None):
    return [('state', '=', 'in')]
on 8/24/15, 9:17 AM

then it should work... does it?

on 8/24/15, 9:22 AM

Better, no errors now ;) So what next, how to add the counter into the line ?

Dr Obx
on 8/24/15, 9:39 AM

do you actually have some records with state=in right now?

on 8/24/15, 9:42 AM

Yes, a lot of them ;)

Dr Obx
on 8/24/15, 9:57 AM

if you've done all right, then it should already displaying the counter in the menu... no more actions are required.

on 8/24/15, 9:57 AM

do you inherit additionally "ir.needaction_mixin"? what is your inherit statement?

on 8/24/15, 9:58 AM

_inherit = 'ir.needaction_mixin' @api.model def _needaction_domain_get(self): return[('state','=','in')]

Dr Obx
on 8/24/15, 10:04 AM

you should include _name attribute as well, when you inherit from multiple models then it's necessary, you'll get something like:

_name = "a.base.model.name"
_inherit = ["a.base.model.name", "ir.needaction_mixin"]
on 8/24/15, 10:04 AM

"ir.needaction_mixin" should be additional inherit, not the main one... see comment above

on 8/24/15, 10:05 AM

"a.base.model.name" here is the model name you're extending. if you are NOT extending any model, then just add _name parameter:

_name = "my.new.model.name"
_inherit = "ir.needaction_mixin"
on 8/24/15, 10:12 AM

''class iprodstep_log(models.Model): _name = 'iprodstep.log' _inherit = ["iprodstep.log","ir.needaction_mixin"] ''

Dr Obx
on 8/24/15, 10:13 AM

so if you're extending some model, use first option... but you'll need to add _name in both cases... and do not forget to restart odoo and update module from Settings/Modules... page. normally it should work already

on 8/24/15, 10:15 AM

is the 'iprodstep.log' existing model you're extending? if so, then it's correct... if it's a new model then use second option, _inherit = "ir.needaction_mixin". I do not know your scenario, so I posted all two options

on 8/24/15, 10:22 AM

Whatever i do, either first or second scenario still getting errors :(

class iprodstep_log(models.Model):
    _name = 'iprodstep.log'
    _description = 'iprodstep Log'
    _inherit = ["iprodstep.log","ir.needaction_mixin"]

    def _needaction_domain_get(self):
Dr Obx
on 8/24/15, 10:32 AM

first things first... is the 'iprodstep.log' another model you're extending OR it's a new model you're creating?

on 8/24/15, 10:36 AM

what kind of errors

on 8/24/15, 10:36 AM

iprodstep.log is a model I'm creating. I'll tell you, just need restore everyting and solve the Server 500 error now :)

Dr Obx
on 8/24/15, 10:43 AM
  File "/usr/share/pyshared/werkzeug/serving.py", line 159, in run_wsgi
  File "/usr/share/pyshared/werkzeug/serving.py", line 146, in execute
    application_iter = app(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/openerp/service/server.py", line 285, in app
    return self.app(e, s)
  File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 216, in application
    return application_unproxied(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 202, in application_unproxied
    result = handler(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 1281, in __call__
    return self.dispatch(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 1255, in __call__
    return self.app(environ, start_wrapped)
  File "/usr/share/pyshared/werkzeug/wsgi.py", line 411, in __call__
    return self.app(environ, start_response)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 1413, in dispatch
    ir_http = request.registry['ir.http']
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 339, in registry
    return openerp.modules.registry.RegistryManager.get(self.db) if self.db else None
  File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 339, in get
  File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 370, in new
    openerp.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 351, in load_modules
    force, status, report, loaded_modules, update_module)
  File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 255, in load_marked_modules
    loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
  File "/usr/lib/python2.7/dist-packages/openerp/modules/loading.py", line 152, in load_module_graph
    models = registry.load(cr, package)
  File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 163, in load
    model = cls._build_model(self, cr)
  File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 595, in _build_model
    original_module = pool[name]._original_module if name in parents else cls._module
  File "/usr/lib/python2.7/dist-packages/openerp/modules/registry.py", line 102, in __getitem__
    return self.models[model_name]
KeyError: 'iprodstep.log'

Dr Obx
on 8/24/15, 10:49 AM

error you posted is because 'iprodstep.log' is not an existing model, but new one.. as you're not extending any model, then _inherit = "ir.needaction_mixin" is correct option for you

on 8/24/15, 10:53 AM

Oh i got it, so how can I now assign it to the specified menu ?

Dr Obx
on 8/24/15, 11:21 AM

you do not need to.

on 8/24/15, 11:40 AM

it'll be added automatically. you just need to have separated page for your 'iprodstep.log' models, you need ordinary menu for that, nothing special

on 8/24/15, 11:42 AM

Not really, because at this moment I have a few menus which contain records in state 'in', 'out', 'cancelled' so i have to separate it somehow or create same kind of counters for each state. like in messaging, it separate numbers of messages for specified group. I hope you know what i mean.

Dr Obx
on 8/24/15, 12:10 PM

Rob, for what you what to achieve, you have to add this:
'needaction_menu_ref': ['list_of_other_menu_ids_of_same_object']
to your respective Menu's action's context in xml file....
suppose on menu with id 'a' you will have :
'needaction_menu_ref': ['b', 'c']
suppose on menu with id 'b' you will have :
'needaction_menu_ref': ['a', 'c']
and so on...... Hope this helps you.......

on 8/25/15, 2:28 AM

it doesn't work ;(

    def _needaction_iprodstep_log_pack_menu_get(self):
Dr Obx
on 8/25/15, 5:34 AM

Rob, don't add this part and implement above mentioned functionality in action's context.....
and in _needaction_domain_get() just return [('active', '=', True)]

on 8/25/15, 5:35 AM

Aaaaaa hahahahahah i sorted it. It was quiet simple :)

    def _needaction_domain_get(self):
        return[('state','not in',['draft'])]
and now I can see in each menu a number of orders :) The only problem is.... it update the counter only in one menu if you pass the order to another state. every time you want to know the actual states you have to click on the top bar menu
Dr Obx
on 8/25/15, 5:57 AM

congrats :)

on 8/25/15, 11:19 AM


| 6 4 7
Tbilisi, Georgia
On 8/24/15, 8:41 AM

for count records matching a domain you should use function "search_count", quote from documentation:

search_count(args) → int
Returns the number of records in the current model matching the provided domain.

in your case, it'll be something like,

in v8 style:

count = self.env['model.name.to.search.in'].search_count([('state','=','in')])

in v7 style:

count = self.pool['model.name.to.search.in'].search_count(cr,uid, [('state', '=', 'in')], context=context) 

then in "count" variable you'll have the desired number. adapt it to your case.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 8/24/15, 5:41 AM
Seen: 798 times
Last updated: 8/24/15, 10:55 AM