Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
6478 Zobrazení

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

Avatar
Zrušit
Nejlepší odpověď

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 

Avatar
Zrušit

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

Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
0
lis 21
3930
1
čvc 21
6908
1
dub 17
4167
2
kvě 22
9562
0
lis 21
3829