Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
3 Răspunsuri
11651 Vizualizări

I just want to limit the item that can be input in my

module( let's say 20 lines) which has one2many relationship.

Imagine profil
Abandonează
Autor Cel mai bun răspuns

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.

 

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Cel mai bun răspuns

Hello,

you can use limit in one2many field. like this:

'order_line': fields.one2many('sale.order.line', 'order_id', 'Order Lines', limit=10),

 

Imagine profil
Abandonează
Autor

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!'))