Skip to Content
Menu
This question has been flagged
1 Reply
9225 Views

I have some fields  in a module which i want to access as many2one fields in my module but fails to access the fields.

Here is my code:-

class location_rental(models.Model):
_name = "location.rental"


location_id = fields.Char('Location_id', required=True)
location_name = fields.Char("Location Name")
row = fields.Char("Row")
column = fields.Char("Column")
level = fields.Char("Level")

 
class rental_pet(models.Model):
_name = 'rental.pet'
 


location_id = fields.Many2one('location.rental.location_id', string="Location Id")
tier = fields.Selection(related="location_id.tier", string="Tier")
    row = fields.Many2one("location.rental.row", string="Row")


I am accessing many2one fields as shown in above code but it shows an error. May I get some help?

Avatar
Discard
Best Answer

First of all you must correct the m2o field declarations and to access other fields you must use related like this :

location_id = fields.Many2one('location.rental', string="Location Id")
row = fields.Char(related='location_id.row')
rental_pet_id = fields.Many2one('rental.pet',related='location_id.your_m2o_field_name')
Avatar
Discard
Author

But i want many2one(dropdown) field in "rental.pet" module not a character field. Is there any way to make a Many2one field as related?

I have added an example of related m2o field

Author

It shows an type error "TypeError: Type of related field rental.pet.row is inconsistent with location.rental.row".

You must change the model name to the correct one, I guess location.rental.row
I strongly recommend you to read the technical documentation here or here (official)

Cdlt,

Mohamed


On 26 October 2017 at 05:22, Gautam <gautambothra1603@gmail.com> wrote:

It shows an type error "TypeError: Type of related field rental.pet.row is inconsistent with location.rental.row".

--
Gautam


Sent by Odoo S.A. using Odoo.


Author

I know model should be correct and i have used the correct model name "location.rental.row" and it shows the above error.

Can you put the declaration of the field row in both models : location.rental and location.pet after your correction as I suggest you?

Author

In location.rental:-

row = fields.Char("Row")

In rental.pet:-

row = fields.Many2one("location.rental.row",related='location_id.row', string="Row")

row is declared as a Char so it must be declared as a related char in other models like in my answer row = fields.Char(related='location_id.row') or change its declaration in location.rental as row = fields.Many2one("location.rental.row", string="Row")

Author

It shows a key error "KeyError: 'location.rental.row"

The code I implemented as you suggested:-

In location.rental:-

row = fields.Many2one('location.rental.row',string="Row")

In rental.pet:-

row = fields.Many2one("location.rental.row", related='location_id.row', string="Row")

you must have a model named 'location.rental.row' if so check its name

Author

name of the model is correct.

Related Posts Replies Views Activity
2
Dec 24
55
0
Jul 24
275
0
Aug 23
701
0
May 23
1739
2
Oct 22
1156