跳至内容
菜单
此问题已终结
2 回复
3955 查看

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/