将所有 stock.move ID 添加到所有相关记录链的列表中
Append all stock.move ID's to list from all chain of related records
我在 stock.move
table 中有记录与 stock.move.reconcile
有 One2many
关系,其中 move_to_id
列是 move_to_id
中另一条记录的 ID =13=] table。所以这条链可以有几千条记录。
正如您在我的示例中看到的那样,我遍历所有记录并逐层向下,但正如我之前所说,可能有数千条链接记录,因此我的方法在这里不起作用。
我知道我可能需要在这里使用 while
循环,比如 while there is move_to_ids
,然后我应该循环遍历记录并继续向列表中添加 ID,但我就是做不到弄清楚怎么做。
move1(stock.move ID = 10)
条记录有 One2many
关系,里面有 2 条记录:move_to_ids
(stock.move.reconcile
)
每个 move_to_ids
都有 move_to_id(many2one, 'stock.move' ID = 11)
每条 move_to_id(stock.move, ID=11)
记录都有任意数量的 move_to_ids (
stock.move.reconcile) and each of this
move_to_idsrecords have
move_to_id('stock.move', ID=12)` 等等。
所以基本上我想添加列出所有 move_to_id
ID 10、11、12 等以列出所有相关的 move_to_ids
。
moves_to_recalculate = [10,11,12]
依此类推,直到有 0 个 move_to_ids
可以从中得到 move_to_id
。
class StockMove(models.Model):
_name = 'stock.move'
move_to_ids = fields.One2many(
'stock.move.reconcile', 'move_from_id', string='Move to')
move_from_ids = fields.One2many(
'stock.move.reconcile', 'move_to_id', string='Move From'
)
class StockMoveReconcile(models.Model):
_name = 'stock.move.reconcile'
_description = 'Stock Move Reconcile'
move_to_id = fields.Many2one('stock.move', string='Move To')
move_from_id = fields.Many2one('stock.move', string='Move From')
def recalculate(self):
moves = self.browse(('active_ids'))
moves_to_recalculate = []
for move1 in moves:
#I add my first move in chain to list
moves_to_recalculate.append(move1.id)
#First move have 2 moves_to_ids so i make another loop to add it ID to list
for second_tier_move in move.move_to_ids:
moves_to_recalculate.appen(second_tier_move.move_to_id.id)
# secont tier move has 1 move_to_ids so i do another loop, and add it's ID to list.
for third_tier_move in second_tier_move.move_to_ids:
moves_to_recalculate.appen(third_tier_move.move_to_id.id)
#third_tier_move has another move_to_ids , and so on.
我从其他资源那里得到了答案,但我想我需要 post 在这里,因为答案非常好。
def recalculate_fifo(self):
moves = self.browse(self._context.get('active_ids'))
moves_to_recalculate = moves
current_moves = moves
while current_moves:
current_moves = current_moves.mapped('move_to_ids.move_to_id')
current_moves -= moves_to_recalculate
moves_to_recalculate |= current_moves
我在 stock.move
table 中有记录与 stock.move.reconcile
有 One2many
关系,其中 move_to_id
列是 move_to_id
中另一条记录的 ID =13=] table。所以这条链可以有几千条记录。
正如您在我的示例中看到的那样,我遍历所有记录并逐层向下,但正如我之前所说,可能有数千条链接记录,因此我的方法在这里不起作用。
我知道我可能需要在这里使用 while
循环,比如 while there is move_to_ids
,然后我应该循环遍历记录并继续向列表中添加 ID,但我就是做不到弄清楚怎么做。
move1(stock.move ID = 10)
条记录有One2many
关系,里面有 2 条记录:move_to_ids
(stock.move.reconcile
)每个
move_to_ids
都有move_to_id(many2one, 'stock.move' ID = 11)
每条
move_to_id(stock.move, ID=11)
记录都有任意数量的move_to_ids (
stock.move.reconcile) and each of this
move_to_idsrecords have
move_to_id('stock.move', ID=12)` 等等。
所以基本上我想添加列出所有 move_to_id
ID 10、11、12 等以列出所有相关的 move_to_ids
。
moves_to_recalculate = [10,11,12]
依此类推,直到有 0 个 move_to_ids
可以从中得到 move_to_id
。
class StockMove(models.Model):
_name = 'stock.move'
move_to_ids = fields.One2many(
'stock.move.reconcile', 'move_from_id', string='Move to')
move_from_ids = fields.One2many(
'stock.move.reconcile', 'move_to_id', string='Move From'
)
class StockMoveReconcile(models.Model):
_name = 'stock.move.reconcile'
_description = 'Stock Move Reconcile'
move_to_id = fields.Many2one('stock.move', string='Move To')
move_from_id = fields.Many2one('stock.move', string='Move From')
def recalculate(self):
moves = self.browse(('active_ids'))
moves_to_recalculate = []
for move1 in moves:
#I add my first move in chain to list
moves_to_recalculate.append(move1.id)
#First move have 2 moves_to_ids so i make another loop to add it ID to list
for second_tier_move in move.move_to_ids:
moves_to_recalculate.appen(second_tier_move.move_to_id.id)
# secont tier move has 1 move_to_ids so i do another loop, and add it's ID to list.
for third_tier_move in second_tier_move.move_to_ids:
moves_to_recalculate.appen(third_tier_move.move_to_id.id)
#third_tier_move has another move_to_ids , and so on.
我从其他资源那里得到了答案,但我想我需要 post 在这里,因为答案非常好。
def recalculate_fifo(self):
moves = self.browse(self._context.get('active_ids'))
moves_to_recalculate = moves
current_moves = moves
while current_moves:
current_moves = current_moves.mapped('move_to_ids.move_to_id')
current_moves -= moves_to_recalculate
moves_to_recalculate |= current_moves