Skip to Content
Menu
This question has been flagged
1 Reply
2945 Views

Hello Gents,


Need help for below. 

I need to prevent entering records one2many field( sparepartslines ) in the  same Asset model based on the field 'name' from spareparts_lineitem.

User can enter the same spareparts_lineitem in different Asset

 

class Asset(models.Model):
_name = 'asset'

name = fields.Char(string="Code", required=True)
assetname = fields.Char(string='Asset Name')
sparepartslines = fields.One2many('spareparts_lineitem', 'asset', string='Spareparts')


class Spareparts(models.Model):
_name = 'spareparts'

name = fields.Char(string="Item number", required=True)
description = fields.Char(string='Description')


class Spareparts_lineitem(models.Model):
_name = 'spareparts_lineitem'

name = fields.Many2one('spareparts', string="Item number", required=True)
description = fields.Char(string='Description', related='name.description', store=True, readonly=True)
quantity = fields.Float(string='Quantity', required=True)
quantityinstore = fields.Float(string='Qty in Store')
asset = fields.Many2one('asset', string="Asset")
Avatar
Discard
Best Answer

You can add in your "spareparts_lineitem" model:

_sql_constraints = [
    ('name_asset_uniq', 'unique (name,asset)', 'Please ensure the part name is unique per asset'),
]
Avatar
Discard
Related Posts Replies Views Activity
1
Oct 19
2977
2
Jul 20
3968
0
Jan 20
4102
2
Sep 23
4501
0
Jun 22
1303