Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Parent foreign key in inheritance by delegation

By
Gregoire Huet
on 11/22/13, 2:33 PM 747 views

Can you help me understand how inheritance by delegation works ? I've implemented the following:

# Generic service base model
class business_services(osv.Model):
    _name = "business.services"
    _description = "Business Services"
    _inherit = ['mail.thread', 'ir.needaction_mixin']

    _columns = {
        'name': fields.char('Service name', size=64, required=True),
         ...
    }
business_services()

# Specific service model
class business_service_service1(osv.Model):
    _name = 'business.service.service1'
    _inherits = {'business.services': 'service_id'}

    _columns = {
         'service_id': fields.many2one('business.services', 'Service ID',required=False,ondelete='cascade'),
         ... (other specific service1 columns)
    }
business_service_service1()

class business_service_service2(osv.Model):
    _name = 'business.service.service2'
    _inherits = {'business.services': 'service_id'}

    _columns = {
         'service_id': fields.related('service_id','business_services_id',type="many2one",relation="business.services",string="Service ID",required=False,ondelete='cascade'),
         ... (other specific service2 columns)
    }
business_service_service2()

I'm instantiating a new service thanks to a wizard that opens the proper view :

def action_view_business_services_add(self, cr, uid, ids, context=None):
    switch...
        res = {
            'name': 'Services',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'business.service.service1',
            'view_id': False,
            'type': 'ir.actions.act_window',
 #            'context': "{'service_id':active_id}",
        }
        return res

This works fine, except for the relation between the _inheritsed object and its parent. I read on the doc that when one _inheritS a model, the instantation process populates the parent's table. In my case, the 'service_id' field never gets populated automatically :

  • The field is turned to required=True by the system
  • If I don't place the field, a field is created with name "Automatically created field to link to parent"

My problem is that at the moment, the system asks me to populate the field 'service_id' by hand. I would like it to have its value automatically set.

I see that there is such an implementation in product_template with field 'product_tmpl_id' acting as the foreign key, but i don't understand how it gets populated.

As in service2 in my example, i've seen that some implementations use a 'related' field, but i get no luck this way either. What's the proper way to achieve this ?

Thank you

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 11/22/13, 2:33 PM
Seen: 747 times
Last updated: 3/16/15, 8:10 AM