Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to check a one2many field have no value when I save crm.lead objects

By
Odoo
on 4/21/16, 12:59 PM 352 views

Hi all,

How to check if my one2many field have no value

.when I save a record I make product_id a one2many field require =True.but after  saving this record it will not shows warning it saved.how to avoid this condition

Below attached my code its not showing alert..


class ServiceDetails(models.Model): 
 _name='service.info'
 service_ids=fields.Many2one('crm.lead','Service')
 product_id=fields.Many2one('product.product', 'Service Type',required=True)
 details=fields.Char('Remarks')
class crm_lead(models.Model):
 _inherit='crm.lead'
crm_lead_ids=fields.One2many('service.info','service_ids','Service Details',required=True)

  @api.model
  def create(self, values):
  if self.crm_lead_ids:
  rec = super(crm_lead, self).create(values)
  else:
     raise Warning('You can not add instructor as a attendee')
  return rec
4

Jignesh Mehta

--Jignesh Mehta--
2081
| 6 4 8
Surat, India
--Jignesh Mehta--


Jignesh Mehta
On 4/22/16, 12:20 AM

Hello Dep,


Try this :-


from openerp import models, fields, api, _

from openerp.exceptions import UserError

@api.model

def create(self, vals):

    result = super(crm_lead, self).create(vals)

    if not result.crm_lead_ids:

        raise UserError(_('You Can not Add Instructor as a attendee.')

    return result


Hope it will works for you.

Thanks,

Thanks.. Jignesh.. why we used result = super(crm_lead, self).create(vals)

Odoo
on 4/22/16, 12:36 AM

also self.crm_lead_ids: this one and result.crm_lead_ids: this are same right?

Odoo
on 4/22/16, 12:37 AM

Hello Dep, create method is in the base so if we access any existing method, we must call super for it.

Jignesh Mehta
on 4/22/16, 12:38 AM

ok Thanks..

Odoo
on 4/22/16, 12:47 AM

Dep,
Infact Jignesh method is correct, but if you still want to minimize the execution then we can just check,
if not vals.get('crm_lead_ids', False):
raise UserError(_('Your message.'),

before calling super,

Pawan
on 4/22/16, 1:09 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/21/16, 12:59 PM
Seen: 352 times
Last updated: 4/22/16, 12:34 AM