2017-04-26 86 views
1

如何在form_serial_no_id模型的plot.allocate模型的Many2one字段中將狀態字段的默認值formdownload模型記錄更改爲true?如何更改odoo中另一個型號的默認字段值

我已經寫了一個方法來覆蓋PlotAllocation模型的odoo創建函數,但它仍然沒有將狀態字段的值更改爲true。相反,它會轉到父類(plot.allocate)中create方法的override函數。無論何時我保存記錄,控制都轉到父方法的創建函數,並忽略繼承plot.allocate模型的類PlotAllocation的創建函數。我怎樣才能解決這個問題?

from openerp import models, fields, api 


class CompanyName(models.Model): 
    _name = 'companyname' 
    _rec_name = 'company_name' 

    company_name = fields.Char(string="Company Name", required=True) 
    company_short_code = fields.Char(string="Company short code", required=True) 


class FormDownload(models.Model): 
    _name = 'formdownload' 
    _rec_name = 'form_serial_no' 

    name = fields.Many2one('companyname', string="Company Name", ondelete='cascade', 
          required=True) 
    form_serial_no = fields.Char(string="Form Serial No", readonly=True) 
    status = fields.Boolean(string="Status", default=False) 

    @api.model 
    def create(self, vals): 
     serial_no = self.env['ir.sequence'].get('formdownload.form_serial_no') 
     code = self.env['companyname'].browse(vals['name']).company_short_code 

     # capitalize company short code values 
     code = code.upper() 

     # merge code and serial number 
     vals['form_serial_no'] = code + serial_no 
     return super(FormDownload, self).create(vals) 


class PlotAllocation(models.Model): 
    _inherit = 'plot.allocate' 

    form_serial_no_id = fields.Many2one('formdownload', 
             domain=[('status', '=', False)], 
             ondelete='cascade', string="Form Serial No") 

    def create(self, vals): 
     form_id = vals['form_serial_no_id'] 
     form_obj = self.env['formdownload'].browse(vals['status']) 

     if form_id: 
      form_obj.write({'status': True}) 

     return super(PlotAllocation, self).create(vals) 

這裏是父類的創建覆蓋功能

class plot_allocate(osv.osv): 
    _name = 'plot.allocate' 
    _inherit = ['mail.thread', 'ir.needaction_mixin']  

def create(self, cr, uid, vals, context=None): 
     offer_dt = vals.get('offer_dt') 
     offer_price = vals.get('offer_price') 
     plot_no = vals.get('plot_no') 
     payment_plan = vals.get('payment_plan') 
     # generate and store the offer letter reference number 
     vals['offer_letter_reference_number'] = self.pool.get(
      'ir.sequence').get(cr, uid, 'offer_letter_reference_number') 
     # override creation method to prevent creation of duplicate plots 
     if plot_no: 
      plot_no_duplicate = self.search(
       cr, uid, [('plot_no', '=', plot_no)]) 
      if plot_no_duplicate: 
       raise osv.except_osv(
        _('Duplicate!'), 
        _('Plot No is allocated or is open for allocation already')) 
     # change status of unallocated plots to allocated in unit_master 
     unit_master = self.pool.get('unit.master').browse(cr, uid, plot_no) 
     # change corresponding plot in unit_master to allocated 
     unit_master.write({'status': 'allocated'}) 
     project_and_plot_id = super(
      plot_allocate, self).create(
      cr, uid, vals, context=context) 
     plot = self.browse(cr, uid, project_and_plot_id, context=context) 
     if payment_plan: 
      self.calc_payment_plan(cr, uid, plot, offer_dt, payment_plan) 
     return project_and_plot_id 
+0

您正在比較boolean與整數,它有意義嗎? '如果vals ['form_serial_no_id'] ==狀態:' –

+0

vals ['name']在這裏是什麼? –

+0

哪個版本的odoo你在用這個? – Bhuro

回答

1

試試這個:

@api.model 
def create(self, vals): 
    status = self.env['formdownload'].browse(vals['form_serial_no_id'])   
    if self.form_serial_no_id.id == status.status: 
     status.write({'status': True})  
    return super(PlotAllocation, self).create(vals) 
+0

感謝您的迴應,但沒有任何變化。這仍然是一回事。 – John

+0

我意識到在父類(plot.allocate)中有一個創建方法的重寫函數,所以無論何時我保存記錄,控制都轉到父方法的create函數,並忽略繼承類「PlotAllocation」的創建函數plot.allocate模型。我怎樣才能解決這個問題? – John

+0

匹配返回超級(class_inherit_name,self).create(vals),因爲它將覆蓋默認方法。 – Rinaldi

0

我想這代碼,它工作得很好:

@api.model 
def create(self, vals): 
     rec_id = super(PlotAllocation, self).create(vals) 
     if rec_id.form_serial_no_id : 
      # raise exceptions.ValidationError('Your code is here should make the status of companyname to true') 
      # print 'Your code is here should make the status of companyname to true' 
      rec_id.form_serial_no_id.status = True 
     return rec_id 
+0

添加到註釋行如果你可以看到打印在控制檯取消註釋打印行,否則取消註釋提升行只是爲了看看代碼是否真的讓你的創建方法變冷靜,因爲我試過這個,它工作得很好。 – Cherif

+0

您是否在嘗試代碼時收到了消息? – Cherif

+0

不,我沒有收到任何消息。 – John

0

試試這個:

@api.model 
def create(self, vals): 
    form_id = vals['form_serial_no_id'].id 
    form_obj = self.env['formdownload'].browse([form_id]) 

    if your condition: 
     form_obj.write({'status':True}) 

    return super(PlotAllocation, self).create(vals) 
+0

感謝您的迴應,這仍然是一回事。什麼都沒有發生 – John

+0

如果你告訴我你到達的地方,它會幫助 –

+0

我剛發現現在在父類(plot.allocate)中有一個創建方法的重載函數,所以任何時候我保存記錄,控制轉到父方法的創建函數並忽略繼承plot.allocate模型的類「PlotAllocation」的創建函數。我怎樣才能解決這個問題?我編輯了這個問題,並在父類中添加了創建函數供您查看。 – John

相關問題