コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
2441 ビュー

here are my Models, I have created a field parent_type in donation.categories which select multiple values (Many2many) from donation.types


now in model mytests i want:

after user select from Many2one field donation.category 

the field category_type should be filled with related records from field parent_type which is Many2many in donation.categories model, 


please help.


class DonationTypes(models.Model):

_name = 'donation.types'

_description = 'Donation Types'


name = fields.Char(string='Donation Type')

colors = fields.Integer(string="Color")


class DonationCategories(models.Model):

_name = 'donation.categories'

_description = 'Donation Categories'


name = fields.Char(string='Donation Category')

parent_type = fields.Many2many('donation.types', string='Types')



class Tests(models.Model):

_name = 'mytests'

_description = 'Tests For'


name = fields.Char(string='My Test')

donation_category = fields.Many2one('donation.categories')

category_type = fields.Many2one( ) -- here i need help


regards



アバター
破棄
最善の回答

Hi,

You can add a computed field , using a related Many2many field from the donation.categories model.



 

thanks

アバター
破棄
著作者

sorry for inconvenience, category_type is Many2one type field as you can see in mytests Model.

category_type = fields.Many2one('donation.types', string='Category Type', compute='_compute_category_type', store=True)

@api.depends('donation_category')
def _compute_category_type(self):
for test in self:
if test.donation_category:
test.category_type = test.donation_category.parent_type and test.donation_category.parent_type[0] or False
else:
test.category_type = False

try this

著作者

related records from Many2many field to Many2one.

you want to update value from many2many field to many2one?

著作者

yes, I want to update from many2many to many2one, this is what i mentioned twice and don't know who was in very hurry to mark your Answer, they should be wait for feedback or a reasonable time period to mark Best Answer. anyway, as mentioned :

I want to update from many2many field to many2one

when user Select a Category in mytests field donation_category the related records from donation.categories field parent_type should be pupulated in mytests field category_type to select one.

hope it cleared now.

regards

what if there is multiple values in many2many field?

著作者

hi, @Mehjabin Farsana(Metalinks IT Consulting&Solutions)

yes there are multiple values and this is why we use many2many field.

i have modified as below and it works with multiple values... but please check if there is anything needs to consider and i missed / overlooked.

-----------
donation_category_id = fields.Many2one('donation.categories', string='Donation Category')
category_type_id = fields.Many2one('donation.types', string='Donation Type')

@api.onchange('donation_category_id')
def _onchange_donation_category(self):
if self.donation_category_id:
parent_type_values = self.donation_category_id.parent_type.ids
self.category_type_id = False
return {'domain': {'category_type_id': [('id', 'in', parent_type_values)]}}
else:
self.category_type_id = False
return {'domain': {'category_type_id': []}}
-----------

regards

関連投稿 返信 ビュー 活動
3
8月 25
2454
1
5月 25
2578
1
4月 25
3540
1
4月 25
4387
1
4月 25
1850