从代码返回空列表创建合作伙伴

问题描述

我有一个自定义模块,我想从中创建新的合作伙伴

 class CustomerRegist(models.Model):
_name = "customer.regist"
_description = 'Customer Registration'
_inherit = ['mail.thread','mail.activity.mixin']
_rec_name='name'
_order='phone'

# def action_pass(self,vals_list):
#     return self.action_confirm(self,vals_list)
def action_done(self):

    if self.full_name:

        vals = {
        'name': self.full_name,'street': self.address_lineone,'street2': self.address_linetwo,'country_id': self.country_id.id,'city': self.city_id.id,'state_id': self.state_id.id,'zip': self.post_code,'phone': self.phone,'email': self.email,'customer_rank': 1,'supplier_rank': 0,'company_type': 'company',}

        self.state = 'active'
        self.env['res.partner'].create(vals)
        return True
    else:
     return False
full_name= fields.Char(string='full_name')
address_lineone = fields.Char(required=True,string='Address')
address_linetwo = fields.Char(string='Line 2')
city_id = fields.Many2one('country.city',required=True,string='City',help='Enter City')
post_code = fields.Char(required=True,string='Post Code')
state_id = fields.Many2one("res.country.state",string='State',help='Enter State',ondelete='restrict')
country_id = fields.Many2one('res.country',string='Country',help='Select Country',ondelete='restrict')
phone_code = fields.Integer(string='Tel',related='country_id.phone_code',store=True)
phone = fields.Char(string='Phone')
email = fields.Char(required=True,string='Email')

class ConfirmWizard(models.TransientModel):
_name='confirm.wizard'
def action_confirm(self):
  return self.env['customer.regist'].action_done()

但是函数返回警告(联系人需要一个名字),或者有时在 res.partner 中创建了一个空记录,名称为 False

任何帮助,谢谢

解决方法

不完全确定您的工作流程是什么。但似乎 self.full_name 可能是空的。为了避免空洞,我们可能会尝试使用电子邮件地址或静态词 Dummy 或任何静态词。例如,

def action_done(self):

    name=self.full_name or self.emaiil or "Dummy"

    vals={
            'name': name,'street': self.address_lineone,'street2': self.address_linetwo,'country_id': self.country_id.id,'city': self.city_id.id,'state_id': self.state_id.id,'zip': self.post_code,'phone': self.phone,'email': self.email,'customer_rank': 1,'supplier_rank': 0,'company_type': 'company',}

    self.state = 'active'
    self.env['res.partner'].create(vals)

    return True
,

已解决

  def action_confirm(self):

  partner = self.env['customer.regist'].browse(self.env.context.get('active_ids'))

  vals = {
      'name': partner.full_name,'street':partner.address_lineone,'street2': partner.address_linetwo,'country_id': partner.country_id.id,'city': partner.city_id.id,'state_id': partner.state_id.id,'zip': partner.post_code,'phone': partner.phone,'email': partner.email,}

  # self.state = 'active'
  self.env['res.partner'].create(vals)
  partner.state='active'