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

Hello Guys

I have declared a many2one field

packaging_id = fields.Many2one(
        'product.packaging', 'Package type', compute='_compute_package_type',
        index=True)

So I need to make a logic to return what objects I want to show in the  list. I have tried it using a compute option but I am not sure this is correct. Follow my compute field

@api.multi
    def _compute_package_type(self):
        pckdel = self.batch_id.package_delivery_group
        order = self.env['ord.data'].search([("pckdelgrp", "=", pckdel)], limit=1)
        type = self.env['delivery.carrier'].search([("name", "=", order.deliverymethod)], limit=1)
        if type.delivery_type == 'test' or type.delivery_type == 'xxx':
            package_objs = self.env['product.packaging'].sudo().search([])
        else:
            package_objs = self.env['product.packaging'].search([("packager_carrier_type", "=", type.delivery_type)])
        self.packaging_id =  package_objs.ids

However it fails and does  not appear the dropdown list with the  package_objs. Could someone please help me on that?

THANK U SO MUCH


Avatar
Discard
Best Answer

Hi, Guilherme


In Many2one field you can add single record only, you can't add multiple records. For more reference you can refer to the Odoo User Guide. In case you want to deal with the multiple records then you can use Many2many or One2many fields.

Odoo Relational Fields 

But as per your code it seems you want to filter the list which shows while selecting a record in Many2one filed. So you can achieve this part by adding domain.

You can define field like that:

packaging_id = fields.Many2one(
        'product.packaging', 'Package type',  domain='_domain_package_type',
        index=True) 
Domain method will be like that

    @api.model
    def _domain_package_type(self):
        pckdel = self.batch_id.package_delivery_group
        order = self.env['ord.data'].search([("pckdelgrp", "=", pckdel)], limit=1)
        type = self.env['delivery.carrier'].search([("name", "=", order.deliverymethod)], limit=1)
        if type.delivery_type == 'test' or type.delivery_type == 'xxx':
            package_objs = self.env['product.packaging'].sudo().search([])
        else:
            package_objs = self.env['product.packaging'].search([("packager_carrier_type", "=", type.delivery_type)])
        packageIds =  package_objs.ids 
        return [('id', 'in', packageIds)]
Hope above code is clear to you. Feel free to ask in case you have any doubt related to the above code.

Thanks,

Ashish Singh (Team Lead)

Webkul Software Private Limited
Avatar
Discard
Best Answer

HI,

Update your compute function like this and see, whether it solve the issue, you have to give limit=1 along with the search, or somehow in the package_obj(in the below code) variable in the code, must hold only a single record, so that we can write the value into Many2one field.



@api.multi
def _compute_package_type(self):
pckdel = self.batch_id.package_delivery_group
order = self.env['ord.data'].search([("pckdelgrp", "=", pckdel)], limit=1)
type = self.env['delivery.carrier'].search([("name", "=", order.deliverymethod)], limit=1)
if type.delivery_type == 'test' or type.delivery_type == 'xxx':
package_obj = self.env['product.packaging'].sudo().search([], limit=1)
else:
package_obj = self.env['product.packaging'].search([("packager_carrier_type", "=", type.delivery_type)], limit=1)
if package_obj:
self.packaging_id = package_obj.id


Thanks

Avatar
Discard