Skip to Content
Menu
This question has been flagged
2 Replies
1144 Views

Hello,

I'm getting a singleton error in if-else condition and would like to add for loop to run code smoothly. The error is arising at 

'partner_id': rec.partner_ids.id,

in which partner_ids is many2many. The action code is as below

def action_create_purchase_order(self):
"""create purchase order and internal transfer"""
for rec in self.requisition_order_ids:
if rec.requisition_type == 'internal_transfer':
self.env['stock.picking'].create({
'location_id': self.source_location_id.id,
'location_dest_id': self.destination_location_id.id,
'picking_type_id': self.internal_picking_id.id,
'requisition_order': self.name,
'move_ids_without_package': [(0, 0, {
'name': rec.product_id.name,
'product_id': rec.product_id.id,
'product_uom': rec.product_id.uom_id,
'product_uom_qty': rec.quantity,
'location_id': self.source_location_id.id,
'location_dest_id': self.destination_location_id.id,
})]
})
else:
self.env['purchase.order'].create({
'partner_id': rec.partner_ids.id,
'requisition_order': self.name,
'project_requisition_name': self.project_requisition_name_id.name,
'job_number': self.job_num,
"order_line": [(0, 0, {
'product_id': rec.product_id.id,
'product_qty': rec.quantity,
'name': rec.description,
})]})
self.write({'state': 'purchase_order_created'})

Thank you in advance.

Avatar
Discard
Best Answer

Hi,

In the partner_ids fields that you are calling in the else condition will have a list of records, that's the reason for the singleton error.If you want to create a purchase order for all the partners in the list, you can change your code in the else condition in to:else:
            for partner_id in rec.partner_ids:
                self.env['purchase.order'].create({
                    'partner_id': partner_id.id,
                    'requisition_order': self.name,
                    'project_requisition_name': self.project_requisition_name_id.name,
                    'job_number': self.job_num,
                    "order_line": [(0, 0, {
                        'product_id': rec.product_id.id,
                        'product_qty': rec.quantity,
                        'name': rec.description,
                    })]})

If you don't need to loop through the partner_ids and only need to select one partner from the list, you can access it using indexes.
else:
                self.env['purchase.order'].create({
                    'partner_id': rec.partner_ids[0].id,
                    'requisition_order': self.name,
                    'project_requisition_name': self.project_requisition_name_id.name,
                    'job_number': self.job_num,
                    "order_line": [(0, 0, {
                        'product_id': rec.product_id.id,
                        'product_qty': rec.quantity,
                        'name': rec.description,
                    })]})


Hope it helps

Avatar
Discard
Best Answer

Hi,

Here the problem is that the partner_id field in the purchase order model is Many2one and rec.partner_ids field is a many2many field.

So when there is multiple values inside the rec.partner_ids and when you try to access the rec.partner_ids.id you will get single ton error, you can resolve it by passing ids instead of id, ie, rec.partner_ids.ids which will give list of ids, but still you cannot write into the partner_id field.

rec.partner_ids.id --- will give error when there is multiple partner
rec.partner_ids.ids ---- [2,5,6]

So depending on your need you have to create multiple purchase order for each partners or select one of the partner from the partner_ids and create the po.

For now change as follows: rec.partner_ids[0].id and see if this solves your issue technically and functionally

Thanks

Avatar
Discard
Related Posts Replies Views Activity
0
Sep 24
1042
0
Apr 24
985
0
Apr 24
616
3
Dec 23
5341
2
Mar 23
2668