콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
8412 화면

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
아바타
취소
베스트 답변

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)

작성자

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

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

작성자

ok Thanks..

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,

What is the syntax for odoo 13?