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

Hi there,

I have a list of records created in this model:


class VLabelProductionSite(models.Model):

    _name = "vlabel.production.site"

    audit_ids = fields.One2many('vlabel.audit', 'related_production_site', string='Audit ID')

    state = fields.Selection([

        ('new', 'New'),

        ('wip', 'Audit in Progress'),

        ('done', 'Audit Done')

    ], string='Audit Status', default='new', track_visibility='always')


This other model looks like this and has these fields:

class VLabelAudit(models.Model):

    _name = "vlabel.audit"

    audit_date = fields.Date(string="Audit Date")

    related_production_site = fields.Many2one('vlabel.production.site',

                                              string='Production Site', required=True)


In a tree view for vlabel.production.site I want to show the date of the audit_id that has the lastest audit_date, filtered for self. If I have three records in vlabel.production.site I want in the tree view for each record to show that latest audit date.


So if there is record1 in vlabel.production.site and it has 4 related records in the vlabel.audit, I want to show the audit_date from the audit that happened last for that record (self)


I tryied for hours trying to get related records of self but didn't manage, this is my solution so far in the vlabel.production.site model


def get_last_audit_date(self):

        for rec in self:

            audits = self.env['vlabel.audit'].search([('related_production_site', '=',          production_site.id), ('state', '=', 'done')])

            last_audit = audits[-1]

        return last_audit.audit_date

 

I am very grateful for any help. Happy holidays to you all.

아바타
취소
베스트 답변

Hello Adrian,

I would suggest doing the date as a computed field as such:

last_audit_date = fields.Date(string="Last Date", compute="_last_date_compute")
@api.depends("audit_ids.audit_date")
def _last_date_compute(self):
    for record in self:
        record.last_audit_date = max([audit.audit_date for audit in record.audit_ids])

This will get the last audit date of all the records in audit_ids. 

I hope this solves your issues.

Thanks, 

아바타
취소
작성자

Thx alot Jack, you already made my day. Way better than forcing the search. All the best,

Adrian

관련 게시물 답글 화면 활동
3
10월 23
4220
1
3월 23
2376
0
12월 22
2998
0
6월 21
2929
0
6월 20
5436