python - counter 方法计数不好

标签 python odoo

我有一个方法可以告诉 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/

相关文章:

python - python 的新 'pip wheel' 是否支持为 tests_requires 中列出的依赖项构建轮子?

javascript - PostgreSQL 物化 View 在刷新时阻塞读取

python - nose 的 assert_raises 函数在哪里?

macos - 如何在 docker 容器中将 VS code 调试器与 odoo 结合使用 [vs code + odoo + docker + mac]

python - 无法适应类型 'account.analytic.account

python - ArrayField 中的列表实例有效吗?

javascript - 有没有可以安全地解释混淆的 javascript 字符串的 python 库?

python - 属性错误: 'NoneType' object has no attribute 'id' - Odoo v8

xml - 如何在表单 View 中使用单个标签在一行中显示两个字段?

xml - Odoo:字段上的条件不可见属性仅在一个方向上有效?