为了账号安全,请及时绑定邮箱和手机立即绑定

Odoo:一个2多个字段仍然添加记录,即使我提出错误,如果它上的零库存

Odoo:一个2多个字段仍然添加记录,即使我提出错误,如果它上的零库存

青春有我 2022-08-02 16:10:51
所以这个想法是当我改变产品时,它会检查当前分支上可用的库存,如果没有,则会引发没有库存的错误预期行为:当所选产品上没有库存时,将引发错误,如果没有库存可用,则不会将记录添加到树视图中当前行为:当所选产品上没有库存可用但记录仍被添加到 one2many 树视图中时,将引发错误代码如下# -*- coding: utf-8 -*-from docutils.nodes import linefrom odoo import models, fields, api, _from odoo.exceptions import ValidationErrorclass CustomTransRequest(models.Model):    _name = 'custom.trans.request'    _description = 'Transfer Request'    user_id = fields.Many2one('res.users', string='Salesperson', default=lambda self: self.env.user)    branch_from_id = fields.Many2one('custom.branch', string="From", required=True)    branch_to_id = fields.Many2one('custom.branch', string="To", required=True)    product_ids = fields.One2many(comodel_name="custom.trans.line", inverse_name="request_id", string="Products",                                  required=False, )class CustomTransLine(models.Model):    _name = 'custom.trans.line'    _description = 'Transfer Request Line'    branch_from_id = fields.Many2one(comodel_name="custom.branch", string="Branch", )    request_id = fields.Many2one("custom.trans.request", string="Request ID", )    product_id = fields.Many2one("custom.product", string="Product", required=True, )    qty = fields.Integer(string="Qty")    @api.onchange('product_id')    def onchange_product(self):        if self.product_id:            for rec in self:                selected_lines = rec.env['custom.branch.line'].search(                    ['&', ('product_id', '=', rec.product_id.id), ('branch_id', '=', rec.branch_from_id.id)]).mapped(                    'qty')                if not selected_lines:                    raise ValidationError(                        _('Current branch has ZERO stock on this product, Please select an available Product'))
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

如果您不希望将记录记录放入一个 2 多个字段,请设置 .并返回验证字典。product_id=False


请尝试使用以下代码。


@api.onchange('product_id')

def onchange_product(self):

    if self.product_id:

        res = {}

        selected_lines = rec.env['custom.branch.line'].search(

            ['&', 

             ('product_id', '=', self.product_id.id), 

             ('branch_id', '=', self.branch_from_id.id)]).mapped('qty')

        if not selected_lines:

            self.product_id = False

            res['warning'] = {'title': _('ValidationError'),

                              'message': _(

                                      'Current branch has ZERO stock on this product, '

                                      'Please select an available Product')}

            return res


查看完整回答
反对 回复 2022-08-02
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号