跳至内容
菜单
此问题已终结
2 回复
2855 查看

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


形象
丢弃
最佳答案

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

形象
丢弃
编写者

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.

编写者

ok, i will try this method.

Thanks fo answer

最佳答案

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.")


形象
丢弃
相关帖文 回复 查看 活动
1
8月 23
29190
3
4月 21
8648
2
6月 20
5816
1
10月 19
4755
2
5月 17
3490