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

I am trying to compute the days since of a book release date. while creating a new record, the code showing an error.

Something went wrong !
library.book(,).age_days

I would like to solve this problem using filtered(lambda function).

My code:
date_release = fields.Date('Release Date', copy=False)
age_days = fields.Float(string='Days Since Release', compute='_compute_age')

@api.depends('date_release')

def _compute_age(self): #self: library.book(,)

    today = fields.Date.today() #today: 2021-06-16
    for book in self.filtered(lambda x: x.date_release):
    delta = today - book.date_release
    book.age_days = delta.days

아바타
취소
베스트 답변

if you assign computed filed ' '/0 value to string you will get rid of this ..

in your case write after the today field 

self.age_days = 0


computed fields are make sure that the computed function assign value to field.. because computed function run every time even when record is creating so they create virtual memory for assigning computed value if they fail to assign value it will show warning  like this library.book('memory address here').age_days

for getting rid of this you have assign a value like for int field 0 or for char field empty string 

아바타
취소

Hi,

Its not neccessary that assigning 0 value should be for the field of the current record.

It can be a empty variable too. ## Kinda proper code

베스트 답변

Hi,

1. Newid error occurs when the compute field is not stored and When creating a record, the records of the recordset will be in memory only. At that time, the id of the record will be a dummy ids of type NewId

2. You need to assign empty Value before the Loop
    All the computed fields need to be assigned an empty value at least and its better if you use @api.mulit()

@api.mulit()
@api.depends('date_release')
def _compute_age(self):
    age_days = 0
    for book in self.filtered(lambda x: x.date_release):
        today = fields.Date.today()
        delta = today - book.date_release
        age_days = delta.days
        book.age_days = age_days

Thanks

아바타
취소
관련 게시물 답글 화면 활동
0
11월 21
4224
1
7월 21
7241
1
4월 17
4411
2
5월 22
9791
0
11월 21
4067