콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
3619 화면

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


아바타
취소
작성자 베스트 답변

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

아바타
취소
베스트 답변

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.

아바타
취소