_constraints,无法创建退款发票 Odoo v10 社区

_constraints, cannot create refund invoice Odoo v10 community

我有这个约束:

_constraints = [
    (_unique_invoice_per_partner,
     _('The Document you have been entering for this Partner has already'
       ' been recorded'),
     ['Control Number (nro_ctrl)', 'Reference (reference)']),
]

关于这个字段:

nro_ctrl = fields.Char(
    string='Control Number', size=32, readonly=True, required=True,
    states={'draft': [('readonly', False)]},
    help="Number used to manage pre-printed invoices, by law you will"
         " need to put here this number to be able to declarate on"
         " Fiscal reports correctly.")

如果我创建发票、验证发票并付款(此字段在 account.invoice 模型上),则此约束有效。

但是如果我创建 Refund,它会说一个字段设置不正确:

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: nro_ctrl - nro.ctrl] 

我也有这种方法,理论上应该允许 "copy" 或复制发票,包括该字段:

@api.multi
def copy(self, default=None):
    """ Allows you to duplicate a record,
    child_ids, nro_ctrl and reference fields are
    cleaned, because they must be unique
    """
    # NOTE: Use argument name ids instead of id for fix the pylint error
    # W0621 Redefining buil-in 'id'
    #if default is None:
        #default = {}
    default = self._context.copy() #default.copy()
    default.update({
        'nro_ctrl': None, 
        'supplier_invoice_number': None,
        'sin_cred': False,
        # No cleaned in this copy because it is related to the previous
        # document, if previous document says so this too
        'date_document': False,
        'invoice_printer': '',
        'fiscal_printer': '',
        # No cleaned in this copy because it is related to the previous
        # document, if previous document says so this too
        # loc_req':False,
        'z_report': '',
    })
    return super(AccountInvoice, self).copy(default)

这是我从 v8 社区迁移到 v10 社区的结果。

我不知道这个copy方法是否有必要。

如何在考虑到这一限制的情况下创建退款?我的意思是,使用 nro_ctrl 字段。

有什么想法吗?

您已创建新字段 nro_ctrl 并且您已在 py 文件中写入 required=True

当你在 py 文件中写 必填字段 那么它在数据库 table.

中是 必填的

您正在更新 'nro_ctrl' 的复制方法:None。由于这个原因,您在创建时遇到错误,因为 none 值不允许出现在必填字段中。

如果发票中需要nro_ctrl字段,那么您必须在退款的复制方法中提供唯一值。