python 的新方法写在哪里。在现有的 .py 文件中还是我需要创建一个新文件?

Where to write new method of python. Either in existing .py files or I need to create a new one?

我将在包含现有 .py 文件的销售点中编写一个方法。我应该创建新的 python 文件吗?或者在现有的 .py 文件中编写新方法??

如果您需要向特定模型添加新方法(例如 sale.order),则继承该特定模型并将您的方法添加到单独的模块中,即自定义模块。

class SaleOrder(models.Model):
    _inherit='sale.order'
    @api.multi
    def custom_test_method(self...)

注意: 这是为了迁移到新版本或从 github 更新您的代码。大多数情况下,对模型的任何修改都只需要在 自定义模块中完成。

切勿更改 基本模块 中的代码,否则该模块不是您编写的。因为当过渡到更新最新代码以获得新功能或迁移到另一个版本时,很有可能会丢失代码并导致奇怪的行为。

为新方法使用自定义模块或覆盖现有方法 例如:要在 pos 模块中添加新方法,模型 "pos.order":

class pos_order(orm.Model):
    _inherit = "pos.order"

    def your_new_method(self, cr, uid, ids, args, context=None):
        ## your code
        return

现有方法:

class pos_order(orm.Model):
    _inherit = "pos.order"

    def your_existing_method(self, cr, uid, ids, args, context=None):
        res = super(pos_order, self).your_existing_method(cr, uid, ids, args, context=context)
        ## your code to change the existing method result
        return res