تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
5341 أدوات العرض

Hello,

Im trying to create the method which has to check the record has selected or not in one2many tree view. If the record has selected then the method will change state. If not, then it will raise the warning for user.

model_b.py :

name = fields.Many2one('hr.employee')

o_2_m = fields.One2many('model.a', 'keya')

state = fields.Selection([

            ('draf', 'DRAF'),

            ('confirm', 'CONFIRMED'),

            (cancel', 'CANCEL'),

            ],default='draf')


model_a.py :

name = fields.Many2one('hr.employee')

keya = fields.Many2one('model.b')


The method's code like this:

def confirm(self)

    for rec in self:

            mod_b = self.env["model.b"].search([('o_2_m.name', '=', rec.name.id)])

            mod_a = self.env["model.a"].search([('name', '=', rec.name.id)])

            if mod_b.o_2_m.name == mod_a.name :

                mod_b.write({'state': 'confirm'})                

            else:

                return {

                    'warning': {

                        'title': "You forget to select the record for changing in o2m",

                        'message': "Please select as below",

                    },

                }

I have tested with select no select the record via "Add a line" link in one2many tree view of model_b.py. But nothing works.


Please help!

Thank you!

الصورة الرمزية
إهمال
أفضل إجابة

Hi, 

I hope the change of state function must be in model_b.py and add this as a button function in your xml view of model_b. 

Here you can raise error or warning and also move state as you required. Just give a try

from odoo.exceptions import UserError

@api.multi

def confirm(self)

    for rec in self:

            if not rec.o_2_m:

                 raise UserError(_("You forget to select the record for changing in o2m"\nPlease select as below"))               

            rec.state = 'confirm'               

     return True

الصورة الرمزية
إهمال

I changed the function to the model_b because searching a model takes much time when compared to accessing the fields with "." (Dot) . Just for code optimization.

الكاتب

Yes, the change of state in model_b.py. And your code is really good. Thank you, Karthikeyan

أفضل إجابة

Hi,

Try the below code and see whether it helps,

from odoo.exceptions import ValidationError

def confirm(self):
for rec in self:
mod_b = self.env["model.b"].search([('o_2_m.name', '=', rec.name.id)])
if rec.o_2_m:
for line in rec.o_2_m:
line.write({'state': 'confirm'})
else:
raise ValidationError(_('You forget to select the record for changing in o2m.'))


Reference:

Odoo ORM Methods

Thanks

الصورة الرمزية
إهمال
الكاتب

Hi Niyas. Thank you for your support. But the state selection is from the model_b.py. But in your code, the state change via the o2m of model a. And I got the error inform from this.

الكاتب أفضل إجابة

I'd resolved my wish, base on Niyas's support.

The code looks like this:

from odoo.exceptions import ValidationError

def confirm(self):
for rec in self:
mod_b = self.env["model.b"].search([('o_2_m.name', '=', rec.name.id)])
if rec.o_2_m:
          mod_b_state = self.env["model.b"].search([])
           if mod_b_state:
              mod_b_state.write({'state': 'confirm'})
else:
raise ValidationError(_('You forget to select the record for changing in o2m.'))
الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
0
فبراير 21
3788
2
أكتوبر 20
3171
1
أغسطس 19
10577
1
أبريل 25
4018
1
ديسمبر 20
277