NeoVe
NeoVe

Reputation: 3897

Mandatory field on inherited model - Odoo v10 community

I've inherited res.partner model, now, I want to make the vat field, as required and unique.

I know how to do this normally, on a new model for example, but I want the original field to have these attributes.

How can I achieve this?

I think it should be on the view, but I'm not really sure, I don't think it could be done on a easy way via python.

For the uniqueness, I've tried like this:

class ResPartner(models.Model):
    _name = 'res.partner'
    _inherit = 'res.partner'

    fields...
    methods...

    _sql_constraints = [
        ('vat_company_uniq', 'unique(company_id, vat)', '¡ El RIF debe ser único por compañia !'),
    ]

But it doesn't works, I mean, what I don't see, is the fact that this field already exists in original object, so how to "modifiy" it in order to be unique and mandatory?

Upvotes: 1

Views: 1016

Answers (2)

Charif DZ
Charif DZ

Reputation: 14721

class ResPartner(models.Model):
    _inherit = 'res.partner'
    vat = fields...(required=True) //just specify the required attributes

    # but here i don't think it will work because the framework will not be able
    # to add this constrains because it has some duplicated value all ready like null
    # in order to make this work you need to run a query to modify old values then restart the server than odoo should be able to add this constraint check log message to see if the constraint is added
    _sql_constraints = [
        ('vat_company_uniq', 'unique(company_id, vat)', '¡ El RIF debe ser único por compañia !'),
    ]

Upvotes: 1

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11143

Remove _name = 'res.partner' and use only _inherit = 'res.partner'.

Afterward, we have to redeclare vat field with required=True attribute in .py side.

_sql_constraints is good.

Restart Odoo server and upgrade your module. It will work fine.

Upvotes: 1

Related Questions