gpt4 book ai didi

python - counter 方法计数不好

转载 作者:行者123 更新时间:2023-12-01 00:23:18 27 4
gpt4 key购买 nike

我有一个方法可以告诉 worker 进行销售的次数,但它不起作用,因为它只标记我他做了 1 个,但实际上做了 5 个。接下来我留下一个图像和用于指导的代码我。

**class Worker (models.Model):**

_name = 'project_rc.worker'

sales_counter = fields.Integer (string = "Sales made", compute = "get_sales_made")
document_ids = fields.One2many (comodel_name = 'project_rc.document',
inverse_name = 'worker_id', string = 'Invoice')

def get_sales_made (self):

count = self.env ['project_rc.type_movement']. search_count ([('type_movement', '=', 'sale')])

self.counter_sale = count


**class Document (models.Model):**

type_movement_id = fields.Many2one (comodel_name = 'project_rc.type_movement', string = "Movement type")

worker_id = fields.Many2one (asdel_name = 'project_rc.worker', string = "Worker")

**class Type_Movement (models.Model):**

type_movement = fields.Selection ([('purchase', 'Purchase'), ('sale', 'Sale'), ('merma', 'Merma')], string = "Movement type")

document_ids = fields.One2many (comodel_name = 'project_rc.document', inverse_name = 'type_movimiento_id', string = 'Document')

示例图片:https://ibb.co/vs0dw5K

最佳答案

问题来自您的函数 get_sales_made

class Worker(models.Model):
_name = 'project_rc.worker'
sales_counter = fields.Integer(string="Sales made", compute="get_sales_made")
document_ids = fields.One2many('project_rc.document', 'worker_id', string='Invoice')

@api.depends('document_ids')
def get_sales_made(self):
for rec in self:
document = rec.document_ids.filtered(lambda r: r.type_movement_id and r.type_movement_id.type_movement == 'sale')
rec.sales_counter = len(document)


class Document(models.Model):
_name = 'project_rc.document'

type_movement_id = fields.Many2one('project_rc.type_movement', string="Movement type")
worker_id = fields.Many2one('project_rc.worker', string="Worker")


class Type_Movement(models.Model):
_name = 'project_rc.type_movement'

type_movement = fields.Selection([('purchase', 'Purchase'), ('sale', 'Sale'), ('merma', 'Merma')], string="Movement type")
document_ids = fields.One2many('project_rc.document', 'type_movement_id', string='Document')

关于python - counter 方法计数不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com