简体   繁体   中英

How to link custom python function to the create button of form view in odoo

I want to call this python method when user click on 'create' button

def _validate_remaining_area(self):
    for land in self:
        if (land.remaining_area <= 0):
            raise ValidationError('You cannot more plot in this land')

How can I do it?

If by create button, you mean standard odoo Model create method, then override that model's create method and call your method there.

For example ( res.partner model):

from odoo import models, api


class ResPartner(models.Model):
    """Extend to modify create method."""

    _inherit = 'res.partner'

    @api.model
    def create(self, vals):
        """Override to call _validate_remaining_area."""
        record = super(ResPartner, self).create(vals)
        # Assuming this method is defined in res.partner model..
        record._validate_remaining_area()
        return record

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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