Skip to Content
मेन्यू
This question has been flagged
2 Replies
795 Views

I have created a model "dws_contact_types" which has the field "contact_type_ids" which I want to show in a tree view of "res.partner" in the model "project.project". However the contact_type_ids field should be stored in projects and inside projects be related to a line in the tree-view with 'partners'. It should be possible to have a different value for the same partner in different projects. 


So far this is what I have:


In my project.py script:


class DWSProjectContacts(models.Model):
    _inherit = ["project.project"]
    #description: "Contacts, added to the project in a seperate tab."
    dws_project_contact_ids = fields.Many2many('res.partner', string='Contacts',store=True,copied=True)
    location = fields.Char(string='location')

class DWSContacts(models.Model):
    _inherit = ["res.partner"]
    contact_type_ids = fields.Many2many('dws.contact.types', string='Contact type', store=True, copied=True)



Then in the treeview (shortened code):


This works, but the problem is that the value of contact_type_ids is stored in the partner model and therefore will have the same value in an other project. which is not what we want. 

When I put the contact_type_ids field in project.project Odoo complains that there is no field "contact_type_ids" in the res.partner model. 


Any help is appreciated. Thanks up forehand.


Avatar
Discard
Best Answer

Hi,


The contact_type_ids field in res.partner causes the same value to appear across different projects because it is directly added to that model. To fix this, create a new model for these fields and use a one2many relationship. This will prevent duplicate records in res.partner.


.py

class ProjectContactDetails(models.Model):

_name = 'project.contact.details'


project_id = fields.Many2one('project.project', string='Project', required=True)

partner_id = fields.Many2one('res.partner', string='Partner', required=True)

contact_type_ids = fields.Many2many('dws.contact.type', string='Contact Types')


class ProjectProject(models.Model):

_inherit = 'project.project'


project_contact_ids = fields.One2many('project.contact.details', 'project_id', string='Project Contacts')


class ResPartner(models.Model):

_inherit = 'res.partner'


project_contact_ids = fields.One2many('project.contact.details', 'partner_id', string='Project Contacts')


Hope it helps.

Avatar
Discard
Author

This helps,

Thank you. The only thing I have to figure out now is how to prevent Odoo from opening a form view when adding a new line in the tree view in project.project. Like the salesorder lines in the SO I would like to just add a new empty line instead of giving the user the choice form already created "ProjectContactDetails". From that new line users can already fill-in the contact and contact type, so we don't need that form.

Thank you for your help so far.