Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
2 Ответы
3953 Представления

Suppose I have a person who can has multiple addresses. I have made a person class with all personal details and one adress class with all address details, how to add a relationship that one person has many addresses.

class person(osv.osv):
  _name: 'foo'
  _description : 'bar'
  _columns: {
name' : fields.char('Name', size= 64, required = True),
        'age' : fields.integer('Age'),
}
person()

class adress(osv.osv):
  _name : 'foo_a'
  _descrption : 'bar'
  _columns: {
'address' : fields.text('Add.'),
}

how to define my relationships.

Аватар
Отменить
Лучший ответ

You can try this one

from openerp.osv import osv, fields


class halo_person(osv.osv):
    _name: 'halo.person'
    _columns: {
        'name' : fields.char('Name', size= 64, required = True),
        'age' : fields.integer('Age'),
        'address_ids':fields.one2many('halo.address','person_id','Addresses'), 
}


class halo_adress(osv.osv):
  _name : 'halo.address'
  _columns: {
        'address' : fields.text('Where is it?'),


        'person_id': fields.many2one('halo.person', 'Related Person'),
}
Аватар
Отменить

Simple Explanation : You have a person, and many address. so in halo.person class, define one_person->many_address or you can type them as one2many fields.

Otherwise, in halo.address class, you should define many2one fields.

Автор

what is the difference between defining address_ids and not defining it, just using person_id directly?

Sorry, I just saw your question. By defining one2many, odoo will automatically link from both sides. So you will be able to track data from two direction. If you want to look at which are the person's addresses, you can access it by using (browserecord,id).address_ids. If you dont define it. You have to search by using self.pool.get('halo.address').search(cr,uid,[('person_id','=',8)]) Sorry for super late answer

Лучший ответ

I'm not sure why you involve code for this question. You can use the module base_contact: https://code.launchpad.net/~openerp-dev/openobject-addons/7.0-base-contact-xal

Аватар
Отменить
Автор

I am learning how to create new modules.. I want to know how it works.. example is made up just for practice..

You should use the technical memento and recreate the "idea" module for practice. It's a good step-by-step: https://www.openerp.com/files/memento/