0
votes

I am trying to reference form_serial_no field of formdownload model in my inherited model(plot.allocate). Instead of the dropdown to show the computed value of form_serial_no which is company_short_code padded with sequence e.g CCK0000006, it's showing "CCK" which is the value of company_short_code excluding the sequence. In actual fact, it's referencing company_short_code of companyname model instead of the computed value of form_serial_no of formdownload model. And in the database, form_serial_no field is showing the right record which is something like these......ABC0000008, CDG0000003 and so on. This is my question, is there any reason why form_serial_no_id Many2one field of plot.allocate model is not picking the value of form_serial_no Char field of formdownload, instead, it's picking the company_short_code Char field of companyname model?

Secondly, how can i change the status field default value of companyname model record to true when a value is picked in form_serial_no_id Many2one field of plot.allocate model?

Kindly help me look into these.

Below are my code snippet

    # -*- coding: utf-8 -*-

from openerp import models, fields, api

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

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

    @api.model
    def create(self, vals):
        record = self.env['formdownload'].search([('name', 'like', vals.get('name', False))]).form_serial_no
        status = self.env['companyname'].search([('name', 'like', vals.get('name', False))]).status
        if vals.get('form_serial_no_id') == record.id:
            self.env['companyname'].write({status : True})

            return super(PlotAllocation, self).create(vals)
1
when you create a record you don't have any error like Keyerror : name ? because you are asking if vals contain nameCharif DZ
no, i did not have keyerror.Ropo

1 Answers

0
votes

in the view the many2one is always showing the value of the field specified in the Model class by _rec_name :

by default _rec_name = 'name'

so in reality your model is like this :

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

so he will retreive the value of field name name is also a many2one so the value retrieved is the _rec_name of model companyname with is company_short_code:

so i you want to change the value of many2one you have to choice:

if one field is enough than specify the _rec_name = 'form_serial_no'

if you want to combined value like first && last name then override name_get method

@api.multi
def name_get(self):
    """
        change the displayed value on m2o
    """
    result = []
    for record in self:
        result.append((record.id, _(" %s  %s") % (record.first_name, record.last_name)))
    return result

EDITS

in create method is easy just remove all the code in create method and put this two line

rec_id = super(PlotAllocation, self).create(vals)
# create return a record you can acces it's value 
rec_id.form_serial_no_id.name.status = True
return rec_id # you need to return the record