Search code examples
pythonodoo-13

odoo 13: inverse name in comodel


I am totally new to odoo and I am stuck with this problem: I have a module named "comite technique" that has a one2many field with the "promoteurs" model in on other module, so I had to add a many2one field to the "promoteurs" model to make it work, when I added the many2one field, the promoteurs works pretty fine here is the code of models.py in the promoteurs module:

class promoteurinitiative(models.Model):
    _name = 'initiative.promoteur'
    _rec_name = 'prenom'

    civilite = fields.Many2one('res.partner.title')
    nom = fields.Char(string="Nom", required=True)
    prenom = fields.Char(string="Prénom", required=True)
    date = fields.Date(string="Date de naissance", required=True)
    email = fields.Char()
    country_id = fields.Many2one('res.country', string='Country', ondelete='restrict')
    tel = fields.Char(string="Tél", required=True)
    contract_count = fields.Integer()
    comite_id = fields.Many2one('initiative.comitetechnique', string="Comité technique")

But whenever I add the one2many field in the "comite technique" ,change its view, restart odoo server and try to upgrade the module, I get this error: enter image description here to see Here is the code in models.py of the "comite technique" module:

import calendar


class comitetechniqueinitiative(models.Model):
    _name = 'initiative.comitetechnique'
    _rec_name = 'date'
    _description = 'comitetechniqueinitiative'

    date = fields.Date(default=fields.Date.today, string="Date de la comité technique", required=True)
    jour = fields.Char( string='Jour')
    pro = fields.One2many('initiative.promoteur','comite_id')


    @api.onchange('date')
    def _get_day_of_date(self):
        for r in self:
            if r.date :
                selected = fields.Datetime.from_string(r.date)
                r.jour = calendar.day_name[selected.weekday()]

I will be grateful if you can help find a solution to this problem


Solution

  • Problem solved by using inheritance and adding dependencies in manfiest file. In models.py of the "comite technique" module:

    class comitetechniqueinitiative(models.Model):
        _name = 'initiative.comitetechnique'
        _rec_name = 'date'
        _description = 'comitetechniqueinitiative anwar'
    
        date = fields.Date(default=fields.Date.today, string="Date du comité technique", required=True)
        jour = fields.Selection([('lundi', 'Lundi'), ('mardi', 'Mardi'), ('mercredi', 'Mercredi'), ('jeudi', 'Jeudi'),('vendredi','Vendredi'),('samedi','Samedi'),('dimanche','Dimanche')
                                  ], string='Jour')
        promoteur = fields.One2many('initiative.promoteur','comite_id')
    
    class promoteur(models.Model):
    
        _inherit = "initiative.promoteur"
    
        comite_id = fields.Many2one('initiative.comitetechnique', string="Comité technique")
    

    And adding in the manifest file of the same module the dependency to the promoteur module:

      'depends': ['base','promoteurinitiative']