Example code for many2one - problem

Asked by Fernando Shayani

Hi all.

I'm trying to build a module for my needs and I'm experimenting some sample codes.

In this example, I need to build a very simple City-State relationship, but I'm getting a error from the server:

Follow the code:

class city(osv.osv):
    _name = 'city'
    _columns = {
        'cityName': fields.char('City', size=32),
        'State': fields.many2one('state','name'),
    }
city()

class state(osv.osv):
    _name = 'state'
    _columns = {
        'name': fields.char('State name',size=32),
    }
state()

The error in the server is:

ERROR:db.cursor:Programming error: operator does not exist: character varying = integer
LINE 1: ...te".id FROM "state" WHERE (state.name in (1,2,3)...
                                                                                          ^

HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
, in query SELECT "state".id FROM "state" WHERE (state.name in (%s,%s,%s)) ORDER BY id

I believe my code is wrong, but I could not find easy examples on the net to fix it.

Can you help me?

Thanks in advance

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Serpent Consulting Services
Solved:
Last query:
Last reply:
Revision history for this message
Best Serpent Consulting Services (serpent-consulting-services) said :
#1

Fernando,

Follow these things, the problem will be solved:

1. Never name any field in uppercase. All field names should be lower case.
2. State model should be defined earlier than city, because you refer it as a foreign key-many2one.
3. For city model, add _rec_name='cityname' as you don't have any field 'name' on it.
--

class city(osv.osv):
_name=''
_rec_name=Field

The error comes because the system does not find a field called 'name' in city model.

The Following code will work for you.

"""
class state(osv.osv):
    _name = 'state'
    _columns = {
        'name': fields.char('State name',size=32),
    }
state()

class city(osv.osv):
    _name = 'city'
    _rec_name = "cityname"
    _columns = {
        'cityname': fields.char('City', size=32),
        'state': fields.many2one('state','name'),
    }
city()

"""

Hope it helps.

Thanks,
Serpent Consulting Services.
http://www.serpentcs.com

Revision history for this message
Fernando Shayani (shayani) said :
#2

Thanks Serpent Consulting Services, that solved my question.

Revision history for this message
namirister (namirister) said :
#3

i have 2 object fields "make" and "model" in view to manage a fleet of cars.
When i want to put data of a car , i must select a make name, so the seconde fields must repopulate with the model name of the make ok my selection in fied 1 "make".

i can't do that with onchange event. so it will be fun if you anyone have a idea about this manipulation
regards.