Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
3597 Vizualizări

Hi, im trying to modify the model website.track by  adding a many2one field , this field have to pick a contact depending on some URL parameters.

To do that I have inherited the website.track model and added a function. To test the function I used a button to trigger it and works fine. The problem starts when I try to override the create method to trigger the function everytime a record is created. I dont understand why the create function is not doing anything even the log dont show up.

Any help is welcome

Here is mi code: 


import logging
from odoo import fields, models, api

logger = logging.getLogger(__name__)
class visita(models.Model):
    _inherit = 'website.track'


    partner_id = fields.Many2one(comodel_name = 'mailing.contact',string='visitante')
 
    @api.depends("url")
    def reconoce(self):
        for a in self:
            var = a.url
            if "?" in var:
                try:
                    var2 = int(var[var.find('?')+1:var.find('%')])
                    a['partner_id'] = self.env['mailing.contact'].browse(var2) #Code for get an Id from                                                                                                                        the url
                except:
                    a['partner_id'] = False

    @api.model_create_multi
    def create(self,vals_list):
        logger.info("TESTING FUNCION CREATE ")
        result = super(visita, self).create(vals_list)
        result.reconoce()
        return result


Imagine profil
Abandonează
Autor Cel mai bun răspuns

Hi, thanks for answer, I tried with api.model decorator and still not working

Imagine profil
Abandonează
Cel mai bun răspuns

It looks like you are trying to override the create method in the visita model. This method is used to create new records in the website.track model.

To do this, you will need to use the @api.model decorator on the create method. This will tell Odoo that you are trying to override the create method in the website.track model.

Your code should look something like this:

import logging
from odoo import fields, models, api

logger = logging.getLogger(__name__)
class visita(models.Model):
    _inherit = 'website.track'

    partner_id = fields.Many2one(comodel_name = 'mailing.contact',string='visitante')

    @api.depends("url")
    def reconoce(self):
        for a in self:
            var = a.url
            if "?" in var:
                try:
                    var2 = int(var[var.find('?')+1:var.find('%')])
                    a['partner_id'] = self.env['mailing.contact'].browse(var2) #Code for get an Id from the url
                except:
                    a['partner_id'] = False

    @api.model
    def create(self,vals_list):
        logger.info("TESTING FUNCION CREATE ")
        result = super(visita, self).create(vals_list)
        result.reconoce()
        return result

I hope this helps! Let me know if you have any other questions.

Imagine profil
Abandonează