Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
2848 Vistas

need help about minimum input value in field odoo

code below

xml version="1.0"?><xpathexpr="//field[@name='order_line']/tree//field[@name='price_unit']"position="after">
​<fieldname="purchase_price"groups="base.group_user"required="1" min="1"/>
xpath>

but still not work, user can save default value 0.00 of purchase_pirce


Avatar
Descartar
Mejor respuesta

Hi Wimpy,

Write a constraint for the purchase price that prevents records with a purchase value less than or equal to zero.

@api.constrains('purchase_price')
def _check_purchase_price(self):
for record in self:
if record.purchase_price <= 0:
raise ValidationError("Purchase price must be greater than 0.")


Hope it helps,
Kiran K

Avatar
Descartar
Autor

Kiran,
need to write models? or can be written on view xml

Thanks for answer

In models, it will work when you save the purchase order record. You can achieve this using the onchange method if you want to throw the warning before creating the new line.

Autor

ok, i will try this method.

Thanks fo answer

Mejor respuesta

Hi,

In this case you have an option to set the value of the field default as 1


example:


from odoo import models, fields


class YourModel(models.Model):

    _inherit = 'your.model'


    your_float_field = fields.Float(string='Your Float Field', default=1.0)



OR


You can add a validation to it


from odoo import models, fields, api

from odoo.exceptions import ValidationError


class YourModel(models.Model):

    _inherit = 'your.model'


    your_float_field = fields.Float(string='Your Float Field')


    @api.constrains('your_float_field')

    def _check_minimum_value(self):

        for record in self:

            if record.your_float_field

                raise ValidationError("The value of 'Your Float Field' must be at least 1.")


Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
ago 23
29189
3
abr 21
8645
2
jun 20
5814
1
oct 19
4753
2
may 17
3489