如何在Odoo中通过id获取以前的数据或最新的数据? [英] How to get the previous data or the latest data by id in Odoo?
问题描述
我创建了一个计费系统,其中包含 meter_no
、prev_reading
、current_reading
和 consumed
字段.在我的 onchange 方法中,当我选择一个仪表编号时,我的 prev_reading
将自动填充上次保存的 current_reading
的值,其中的值是 meter_no代码>.这是我的 onchange 方法:
I created a billing system where I have a fields of meter_no
, prev_reading
, current_reading
and consumed
.
In my onchange method, when I select a meter number, my prev_reading
will be automatically filled with the value of the last saved current_reading
by id of a meter_no
. Here's my onchange method:
@api.multi
@api.onchange('meter_no')
def onchange_meter_no(self):
ids = self.search([('meter_no','=',self.meter_no.id)])
last_id = ids and max(ids)
if self.meter_no:
self.prev_reading = last_id.current_reading or None
但这总是返回第二个值,而不是最后一个数据.例如,BILL-01 prev_reading = 0.0
、current_reading = 10.0
和 consumed = 10.0
.在 BILL-02 中,数据按预期正确,其中 prev_reading = 10.0
,然后我输入 current_reading = 20
,然后 consumed = 10
.在第三次计费中,BILL-03 prev_reading
始终为 10
,在第四次之前始终相同,依此类推.这有什么问题?我已经使用 max()
来获取最后一个 id.
But this always returns the second value, not the last data. Example, BILL-01 prev_reading = 0.0
, current_reading = 10.0
, and consumed = 10.0
. In BILL-02, the data is correct as expected where, prev_reading = 10.0
, then I entered current_reading = 20
, then consumed = 10
. Here in third billing, BILL-03 prev_reading
is always 10
, it's always the same until fourth and so on. What's wrong with this? I already used the max()
to get the last id.
推荐答案
您可以将选项传递给搜索功能以获取最后一个 id,如下所示:
You could pass options to search function to get the last id like this:
record_ids = self.search([('meter_no','=',self.meter_no.id)], order='id desc', limit=1)
last_id = record_ids.id
...
这篇关于如何在Odoo中通过id获取以前的数据或最新的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!