通过值查找记录的数据库ID - xiaohao0576/odoo-doc GitHub Wiki

db_id_for方法

源代码如下:

https://github.com/odoo/odoo/blob/27cd0a3fea1b47a85a4f3d397b052bbec08e182b/odoo/addons/base/models/ir_fields.py#L478

部分源码

Model: ir.fields.converter

    @api.model
    def db_id_for(self, model, field, subfield, value, savepoint):
        """ Finds a database id for the reference ``value`` in the referencing
        subfield ``subfield`` of the provided field of the provided model.

        :param model: model to which the field belongs
        :param field: relational field for which references are provided
        :param subfield: a relational subfield allowing building of refs to
                         existing records: ``None`` for a name_search,
                         ``id`` for an external id and ``.id`` for a database
                         id
        :param value: value of the reference to match to an actual record
        :param savepoint: savepoint for rollback on errors
        :return: a pair of the matched database identifier (if any), the
                 translated user-readable name for the field and the list of
                 warnings
        :rtype: (ID|None, unicode, list)
        """

代码逻辑

  • 如果一个字段有 /id结尾,就用xml_id去查找数据库记录
  • 如果一个字段有 /.id结尾,就用数据库ID直接去匹配
  • 如果有没 / 和后缀,就直接用模型的name_search去查找,比如产品可以通过产品名、条码、内部参考去搜索