Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1363 Lượt xem

i have been trying to get the records from res.partner model and filled the fiels att depeding upon the record 

here is what i want the outcome.
when the partner_id has the attribute is_company is true from res.partner
want to fill the all user under that country in att field  if its individual then only that company record to be filled in that field so im confused that im using the domain correctly whatso ever but i'm not getting the outcome as expected.
this is code snipet 

partner_id = fields.Many2one('res.partner', string='Partner')
att = fields.Many2one('res.partner', string='Attendee') 

    @api.onchange('partner_id')
    def _onchange_partner_id1(self):
        for rec in self:
            if rec.partner_id:
                if rec.partner_id.is_company:
                    return {
                        'domain': {'att': [('id', 'in', rec.partner_id.child_ids.ids)]}
                    }
                else:
                    return {
                        'domain': {'att': [('id', '=', rec.partner_id.id)]}
                    }
            else:
                return {
                    'domain': {'att': []}
                }


appreciate for helping.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello _ALI  ,


Your code snippet is almost correct, but there are a few adjustments needed to achieve the desired functionality. Specifically, you need to handle setting the domain for the att field based on whether the partner_id is a company or an individual correctly. Here’s how you can adjust your code:


    1. Use Many2many for att field if there can be multiple attendees:

       If you intend to allow multiple attendees (which seems likely given your description),

       you should use Many2many instead of Many2one for the att field.


    2. Handle the domain logic correctly:

       Ensure that the domain logic properly filters records based on the is_company attribute.


       Here’s the revised code:


  //Code in Comment//

Hope this Helps,


If you need any help in customization feel free to contact us.


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

Ảnh đại diện
Huỷ bỏ

Code :

from odoo import models, fields, api

class YourModel(models.Model):
_name = 'your.model'
_description = 'Your Model Description'

partner_id = fields.Many2one('res.partner', string='Partner')
att = fields.Many2many('res.partner', string='Attendees')

@api.onchange('partner_id')
def _onchange_partner_id1(self):
for rec in self:
if rec.partner_id:
if rec.partner_id.is_company:
# If partner is a company, set domain to include all child partners (individuals)
rec.att = [(6, 0, rec.partner_id.child_ids.ids)]
return {
'domain': {'att': [('id', 'in', rec.partner_id.child_ids.ids)]}
}
else:
# If partner is an individual, set domain to include only that partner
rec.att = [(6, 0, [rec.partner_id.id])]
return {
'domain': {'att': [('id', '=', rec.partner_id.id)]}
}
else:
# If no partner is selected, clear the domain
rec.att = [(6, 0, [])]
return {
'domain': {'att': []}
}

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 11 22
4095
2
thg 10 24
1529
3
thg 4 20
10577
1
thg 7 25
560
0
thg 12 20
5066