简体   繁体   English

如何在 Odoo 14 中搜索 res.partner 记录

[英]How to search res.partner records in Odoo 14

I want to add check on field 'Email' in res.partner when confirming sale order.在确认销售订单时,我想在 res.partner 中的“电子邮件”字段上添加检查。

class SaleOrderExtend(models.Model):
    _inherit = 'sale.order'

    def action_confirm(self):
        partner = self.env['res.partner'].search(['partner_id', '=', self.partner_id])
        if partner.email == '':
            raise UserError(_("""Email is empty ."""))
    ....

but I got this error: The above exception was the direct cause of the following exception:但我得到了这个错误:上述异常是以下异常的直接原因:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 639, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 315, in _handle_exception
    raise exception.with_traceback(None) from new_cause
IndexError: tuple index out of range

What's wrong please?请问怎么了? Thanks.谢谢。

Firstly your search domain is wrong.首先,您的搜索域是错误的。 It should be a list of special logical operators as strings and triplets (tuples or lists with 3 values).它应该是特殊逻辑运算符的列表,如字符串和三元组(元组或具有 3 个值的列表)。

I won't describe the domain syntax in detail now, because your code only has a simple mistake: your triplet should be in a list:我现在不会详细描述域语法,因为您的代码只有一个简单的错误:您的三元组应该在一个列表中:

self.env['res.partner'].search([('partner_id', '=', self.partner_id)])
# or as lists in list
self.env['res.partner'].search([['partner_id', '=', self.partner_id]])

Secondly this search won't work, because there is no partner_id field in res.partner model and self.partner_id is a recordset not an ID, which you will need on searches on many2one fields.其次,此搜索不起作用,因为res.partner model 中没有partner_id字段,而self.partner_id是记录集而不是 ID,您在搜索 many2one 字段时将需要它。 In the end what you need is to find the partner of that order you're confirming.最后,您需要找到您正在确认的订单的合作伙伴。

You don't need to search it, because it's already an attribute of self in this case.您不需要搜索它,因为在这种情况下它已经是self的一个属性。

    def action_confirm(self):
        if not self.partner_id.email:
            raise UserError(_("""Email is empty ."""))

Some optimization hints: email addresses can be empty or even only spaces, because it's just a simple string/char field.一些优化提示: email 地址可以是空的,甚至可以只有空格,因为它只是一个简单的字符串/字符字段。 So try to take that into consideration aswell:所以尽量考虑到这一点:

    def action_confirm(self):
        if not self.partner_id.email or not self.partner_id.email.strip():
            raise UserError(_("""Email is empty ."""))

And one more: It is possible to confirm more than one order at once, so try to consider that, too:还有一个:一次确认多个订单是可能的,所以也要考虑一下:

    def action_confirm(self):
        not_valid_orders = self.filtered(
            lambda o: not o.partner_id.email or not o.partner_id.email.strip())
        if not_valid_orders:
            raise UserError(
                _("""Some orders have partners without email address"""))
class SaleOrderExtend(models.Model):
   _inherit = 'sale.order'

   def action_confirm(self):
     if not self.partner_id.email or not self.partner_id.email.strip():
        raise UserError(_("""Email is empty ."""))
   ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM