1
votes

I have 3 models, here is the interesting part of each:

vote:

class vote(osv.osv)

_name = 'vote'

_columns = {
    name = fields.many2one('res.partner', 
                                            'Member of enterprise',
                                            select=True),

    type = fields.interger('Type'),
}

history_line:

class history_line(osv.osv)

_name = 'history_line'

_columns = {
    fieldsA = fields.integer('First field'),
    fieldB = fields.integer('Second field'),
    this_id = fields.many2one('res.partner', 'link to res'),
}

res_partner:

class res_partner(osv.osv)

_inherit = 'res.partner'

_columns = {
    vote_partner_ids = fields.one2many('vote', 
                                            'name',
                                            'Voted peoples',
                                            select=True),

    vote_history_ids = fields.one2many('history.line',
                                              'this_id',
                                              compute='compute_type', 
                                              string='History of votes'),
}

@api.multi
@api.depends('vote_partner_ids.type')
def compute_type(self):
    for record in self:
        if self.vote_partner_ids.type:
            record.vote_history_ids = [(0, 0, {'self.vote_history_ids.fieldA': self.vote_partner_ids.type + 4,
                                                'self.vote_history_ids.fieldB': self.vote_partner_ids.type - 2})]

Their is also a default value for a new history_line (fieldA = 1 when you don't do anything, fieldB = -1 when you don't do anything for exemple)

I can't move my compute function elsewhere because lot of stuffs are computed here.

The problem is : When I modify type in vote, a new history line will be created, but with the defaults values. I can't make the values be anything else. (even If I put directly a value, like 10) So why are the default values choose, even if I asked them to being computed, but it still understand it has to create a new tuple?

1

1 Answers

2
votes

You don't have to make the "PATH" to the fields you'd like to change, remove self.vote_history_ids. in the depends before the fields to change:

@api.multi
@api.depends('vote_partner_ids.type')
def compute_type(self):
    for record in self:
        if self.vote_partner_ids.type:
            record.vote_history_ids = [(0, 0, {'fieldA': self.vote_partner_ids.type + 4,
                                                'fieldB': self.vote_partner_ids.type - 2})]