This question has been flagged

Hi,

I'm trying to create entries from template, for example when user choose template from the list the list below (One2Many) should be crated from the template lines, but I have bad query error plus when the method update the list I can see its not correct, the product and equipment is missing, here's my code:

class cmms_accessory(models.Model):
_name = 'cmms.accessory'
_description = 'Equipment Accessory'

name = fields.Char(related='product.name')
equipment_id = fields.Many2one('cmms.equipment', 'Serial Number', required=True)
product = fields.Many2one('product.product', 'Product', required=True)
photo = fields.Binary(string="Image", related="product.image")
revision_number = fields.Char(string='Revision Number', help='E.g 2.0')
revision_description = fields.Char(string='Revision Description', help='E.g Chogori/ Black Remote')
state = fields.Selection([('ok', 'OK'), ('in_transit', 'In Transit'), ('replaced', 'Replaced'), ('fault', 'Fault'),
('returned', 'Returned')], string='Status', default='ok')
serial = fields.Char(string='Serial Number')
additional_info = fields.Char(string='Additional Information')
Here's the error :
odoo.sql_db: bad query: INSERT INTO "cmms_accessory" ("id", "product", "revision_description", "equipment_id", "state", "revision_number", "serial", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('cmms_accessory_id_seq'), NULL, NULL, 1, '
ok', '1', NULL, 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
and here's my onchange method in different class
@api.onchange('template_id')
def _add_accessories(self):
if not self.template_id:
return

template = self.template_id.accessory_template
accessories_lines = []

for line in template:
aa = line.accessory_template
data = {
'product.id': aa.product.id, # M2O
'revision_description': aa.revision_description, # Char
'equipment_id': self.id, #M2O
'state': 'ok', # Selection
'revision_number': aa.revision_number, # Char
'serial': None, # Char
'photo': aa.photo,#Binary
'additional_info': None,#Char
}

accessories_lines.append((0, 0, data))
self.accessory = accessories_lines;
Avatar
Discard
Author

Fixed.. missed product(.id) and used self.updated. Closed!