如何从odoo中的另一个模型更改默认字段值 [英] how to change the default field value from another model in odoo

查看:752
本文介绍了如何从odoo中的另一个模型更改默认字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在plot.allocate模型的form_serial_no_id Many2one字段中选取值时,如何将formdownload模型记录的状态字段默认值更改为true?

How can I change the status field default value of formdownload model record to true when a value is picked in form_serial_no_id Many2one field of plot.allocate model?

我已经写了一个方法来覆盖PlotAllocation模型的odoo创建功能,但是它仍然没有将status字段的值更改为true.而是转到父类(plot.allocate)中create方法的重写功能.每当我保存记录时,控制权就转到父方法的create函数,而忽略继承plot.allocate模型的类PlotAllocation的create函数.我该如何解决?

I have written a method to override odoo create function of the PlotAllocation model but it's still not changing the value of status field to true. Instead, it goes to the override function of create method in the parent class(plot.allocate). Anytime I save record, control goes to the create function of the parent method and ignore the create function of class PlotAllocation that inherit the plot.allocate model. How can I fix this?

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)

这是父类的创建覆盖功能

Here is the create override function of the parent class

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

推荐答案

尝试一下:

@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)

这篇关于如何从odoo中的另一个模型更改默认字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆