This question has been flagged
4 Replies
8443 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"


Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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

Author

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

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

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

or change it to v7 style:

def _needaction_domain_get(self, cr, uid, context=None):
    return [('state', '=', 'in')]

then it should work... does it?

Author

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

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

Author

Yes, a lot of them ;)

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

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

Author

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

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"]

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

"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"
...
Author

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

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

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

Author

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"]

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

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

what kind of errors

Author

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

Author
  File "/usr/share/pyshared/werkzeug/serving.py", line 159, in run_wsgi
    execute(app)
  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
    update_module)
  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'

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

Author

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

you do not need to.

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

Author

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.

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.......

Author

it doesn't work ;(

    @api.model
    def _needaction_iprodstep_log_pack_menu_get(self):
        return[('in','out','pack')]

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

Author

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

    @api.model
    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

congrats :)

Best Answer


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.

Avatar
Discard