1
votes

i have a model that inherits res.partner model

class supplierDetails(models.Model):
    _inherit = 'res.partner'

   farmer_code = fields.Char(string="Farmer's Code")

Now i want the values from farmer_code in another model, so as per this answer, i reached this far.

class productTest(models.Model):
     _name = 'quality.physical'
     _inherit = 'res.partner'

frm_code_ids = fields.Many2one('res.partner',string="Farmer Code")
    frm_cod = fields.Char(related='frm_code_ids.farmer_code',store=True,readonly=True)

Now i am getting KeyError: 'farmer_code'. What do i do to fix this? Thanks in advance.

1
For making related field you must have 'farmer_code' field in res.partner model. - Avani Somaiya
@AvaniSomaiya , Sorry, copied different field to the question. I've corrected the question. - p.ry
You have to remove '_inherit = res.partner ' line form your productTest class. - Avani Somaiya
@AvaniSomaiya, still same error - p.ry
First, comment on the code of productTest class, restart the service and upgrade your module. After successfully upgrade module uncomment the code of productTest class. And again restart the service & upgrade module. - Avani Somaiya

1 Answers

2
votes

Your code should work, repeat the process step by step make sure the res.partner have this field you may forget to put the class in the __init__.py file.

     # related field without store is like a compute field it's computed on fly
     related_field_name = fields.FieldType(related='your_m2o.target_field_name')

If you put store=True it will be added to the database and recomputed whenever you change the value of many2one.

You can go more than one level ex: your_m2m.another_m2o_field.target_field_name.

Field type should be the same as the target field.

Just make sure the target model have that field.