odoo17的(NewId)创建新记录的伪标识符对象如何实现

Pseudo-ids for new records, encapsulating an optional origin id (actual record id) and an optional reference (any value).

class NewId(object):

    Pseudo-ids for new records, encapsulating an optional origin id (actual

        record id) and an optional reference (any value).

    TRANSLATED SEGMENT:

    __slots__ = ['origin', 'ref']


    def __init__(self, origin=None, ref=None):

        self.origin = origin

        self.ref = ref


    def __bool__(self):

        return False


    def __eq__(self, other):

        return isinstance(other, NewId) and (

            (self.origin and other.origin and self.origin == other.origin)

            or (self.ref and other.ref and self.ref == other.ref)

        )


    def __hash__(self):

        return hash(self.origin or self.ref or id(self))


    def __repr__(self):

        return (

            "<NewId origin=%r>" % self.origin if self.origin else

            "<NewId ref=%r>" % self.ref if self.ref else

            "<NewId 0x%x>" % id(self)

        )


    def __str__(self):

        if self.origin or self.ref:

            id_part = repr(self.origin or self.ref)

        else:

            id_part = hex(id(self))

        return "NewId_%s" % id_part 

This code defines a class named `NewId` that represents a pseudo-identifier for new records. Let me briefly explain:


- The `__init__(self, origin=None, ref=None)` method is used to initialize the object, accepting two optional parameters `origin` and `ref`, which represent the identifier of the original record and the reference value, respectively.

- The `__bool__(self)` method returns `False`, indicating that the boolean value of the object is false.

- The `__eq__(self, other)` method is used to compare whether two `NewId` objects are equal. It first checks whether the other object is also an instance of the `NewId` class, and then compares whether their `origin` or `ref` attributes are equal.

- The `__hash__(self)` method returns the hash value of an object, used for hash-based lookups in sets.

- The `__repr__(self)` method returns the string representation of the object, returning different formatted strings depending on whether the `origin` or `ref` attribute exists.

- The `__str__(self)` method returns the string representation of the object, returning the corresponding identifier string based on whether the `origin` or `ref` attribute exists.


In summary, this class is used to create pseudo-identifier objects representing new records, which can contain the identifier of the original record or any reference value.

关于我们

​我们致力于帮助中小企业实现数字化转型,我们的团队由一群充满激情和创新思维的专业人士组成,他们具备丰富的行业经验和技术专长。

扫一扫获取顾问以及手册

归档
登录 留下评论
什么是ERP
企业资源企划