I just want to limit the item that can be input in my
module( let's say 20 lines) which has one2many relationship.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
I just want to limit the item that can be input in my
module( let's say 20 lines) which has one2many relationship.
I made some dirty hack in js
I added this line:
if(this.o2m.options.add_limit){
if(this.records.length < this.o2m.options.add_limit){
this._super.apply(this, arguments);
}else{
alert('Cannot add an item \nLimit Exceed:'+this.o2m.options.add_limit);
}
}else{
this._super.apply(this, arguments);
}
and this in my view.xml:
<field name="one2many_field' options="{"add_limit":28}" />
So the user cannot add item next of it.
Hi,
Here is a more simpler method,
Use @api.constrains to limit the adding of item in a one2many field
Here is sample code:
class SaleOrder(models.Model):
_name = 'sale.order'
_description = 'Sale Order'
name = fields.Char(string='Name')
sale_lines = fields.One2many('sale.order.line', 'order_id', string='Sale Lines')
@api.constrains('sale_lines')
def _check_sale_lines(self):
for record in self:
if len(record.sale_lines) > 20:
raise ValidationError('Not more than 20 lines')
When a user attempts to add more than 20 lines to a sale order, the constraint will be triggered and prevent the change from being saved.
Regards
Hello,
you can use limit in one2many field. like this:
'order_line': fields.one2many('sale.order.line', 'order_id', 'Order Lines', limit=10),
It will limit only the display but adding can still continue. What I mean is that adding of item should stop after 20 lines. Or the "add item" should disappear.
Hello you can check on create method like this: def create(self, cr, uid, vals, context=None): if len(vals.get('line_ids')) > 3: raise osv.except_osv(_('Warning!'), _('Record limit is over!'))
Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!
Aanmelden
1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.