Odoo Help


How to get count of projects per partner?

Jairo Llopis
on 9/15/14, 4:57 AM 1,657 views

I have this code in my module:

from openerp import api, fields, models

class Partner(models.Model):
    _name = _inherit = "res.partner"

    def _project_count(self):
        self.project_count = len(self.project_ids)

    project_count = fields.Integer(compute="_project_count")
    project_ids = fields.One2many("project.project", "partner_id")

But it yields this exception:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 496, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 513, in dispatch
    result = self._call_function(**self.params)
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 279, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper
    return f(dbname, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 276, in checked_call
    return self.endpoint(*a, **kw)
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 723, in __call__
    return self.method(*args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 372, in response_wrap
    response = f(*args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 941, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 924, in _call_kw
    records = getattr(request.session.model(model), method)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/http.py", line 825, in proxy
    result = meth(cr, request.uid, *args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 234, in wrapper
    return old_api(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/models.py", line 3085, in read
    result = BaseModel.read(records, fields, load=load)
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 232, in wrapper
    return new_api(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/models.py", line 3128, in read
    values[name] = field.convert_to_read(record[name], use_name_get)
  File "/usr/local/lib/python2.7/dist-packages/openerp/models.py", line 5411, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "/usr/local/lib/python2.7/dist-packages/openerp/fields.py", line 707, in __get__
  File "/usr/local/lib/python2.7/dist-packages/openerp/fields.py", line 807, in determine_value
  File "/usr/local/lib/python2.7/dist-packages/openerp/fields.py", line 767, in compute_value
  File "/usr/local/lib/python2.7/dist-packages/openerp/fields.py", line 759, in _compute_value
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 232, in wrapper
    return new_api(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 392, in new_api
    result = [method(rec, *args, **kwargs) for rec in self]
  File "/opt/odoo/extra-addons/grupoesoc/partner_projects/partner_projects.py", line 28, in _project_count
    self.project_count = len(self.project_ids)
  File "/usr/local/lib/python2.7/dist-packages/openerp/fields.py", line 707, in __get__
  File "/usr/local/lib/python2.7/dist-packages/openerp/fields.py", line 798, in determine_value
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 232, in wrapper
    return new_api(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/models.py", line 3167, in _prefetch_field
    result = records.read(list(fnames), load='_classic_write')
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 232, in wrapper
    return new_api(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/models.py", line 3117, in read
  File "/usr/local/lib/python2.7/dist-packages/openerp/api.py", line 232, in wrapper
    return new_api(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/models.py", line 3276, in _read_from_database
    res2 = self._columns[f].get(cr, self._model, ids, f, user, context=context, values=result)
  File "/usr/local/lib/python2.7/dist-packages/openerp/osv/fields.py", line 689, in get
    }, (tuple(record_ids),))
  File "/usr/local/lib/python2.7/dist-packages/openerp/sql_db.py", line 158, in wrapper
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/openerp/sql_db.py", line 234, in execute
    res = self._obj.execute(query, params)
ProgrammingError: column "partner_id" does not exist
LINE 1: SELECT id, partner_id                        FROM project_pr...

Seems like I cannot relate it directly to project.project because the field partner_id comes from account.analytic.account, but then how can I get the relationship between res.partner and project.project?

Jairo Llopis
On 9/15/14, 7:52 AM

The answer is to use a relationship with "account.analytic.account" and filter from there:

from openerp import api, fields, models

class Partner(models.Model):
    _name = _inherit = "res.partner"

    def _project_count(self):
        self.project_count = len(self.env["project.project"].search(
              [c.id for c in self.contract_ids])]))

    project_count = fields.Integer(compute="_project_count")

Ludo - Neobis

--Ludo - Neobis--
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--


Ludo - Neobis
On 9/15/14, 6:37 AM

Project by itself is an analytic account. Looking at the source code, it tells me the following:

    _inherits = {'account.analytic.account': "analytic_account_id",

                 "mail.alias": "alias_id"}

This means that on the project there should be an analytic_account_id field, pointing to the related analytic account. You could use that. Depending on your business needs, it could be a related field or perhaps a one2many field using using related fields to view project names.

Thanks. The problem is that not all analytic accounts are projects, but I will search from there.

Jairo Llopis
on 9/15/14, 6:59 AM

If I recall correctly, the analytic account has a field "type" which indicates what type of account it is. Project should be an option there.

Ludo - Neobis
on 9/15/14, 7:26 AM

It's true, but that field does not say if it is a project or not. I already found the solution, thanks.

Jairo Llopis
on 9/15/14, 7:51 AM

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 9/15/14, 4:57 AM
Seen: 1657 times
Last updated: 3/16/15, 8:10 AM