Odoo Help

0

Fields.function with type one2many in wizard

By
Cyril Sester
on 8/25/14, 10:28 AM 6,416 views

Hi,

I have data with a many2many relation between 2 models. Simplified model is :

Class property(orm.Model):

        _name = 'property'

        _columns = {

                'value_ids': fields.many2many(

                        'value', 'property_value', 'property_id', 'value_id', _('Values'))

        }

Class value(orm.Model):

        _name = 'value'

        _columns = {

                'property_ids': fields.many2many(

                        'property', 'property_value', 'value_id', 'property_id', _('Properties')),

                'field1': fields.char(_('Field 1')),

                'field2': fields.char(_('Field 2'))

        }

class dummy(orm.Model):

       _name = 'dummy'

       _columns = {

            'case_study_ids': fields.one2many(
                  'property', 'dummy_id', string=_('Properties'))

      }

Until here, everything works fine. Now, on my dummy model form view I have a button who launch a wizard who makes some process over values for a specific property (id passed in context). This wizard first shows values associated to property and allows modifications on them. Then, the internal process is done.

class property_process_wizard(orm.TransientModel):
        _name = 'property.process.wizard'
    
        def get_value_ids(self, cr, uid, ids, field_name, arg, context=None):
                property_id = context.get('property_id', False)
                if not property_id:
                        return dict([(id, []) for id in ids])

                value_obj = self.pool.get('value')
                value_ids = value_obj.search(cr, uid, vals = [('property_id', '=', property_id)])
                return dict([(id, value_ids) for id in ids])

 

        _columns = {

                'value_ids': fields.function(get_value_ids, type='one2many',
                                                                                                   obj='value'),

        }

Here is my problem. The field function is not computed when the wizard is launched. Is there a way to force field computation on wizard launching?

Best regards,

 

0
Cyril Sester
On 8/27/14, 8:08 AM

One more time, I will give myself the answer.

It's sadly simple, as it's done with the _defaults. For example : 

...

def get_value_ids(self, cr, uid, ids, field_name, arg, context=None):
        property_id = context.get('property_id', False)
        if not property_id:
                return dict([(id, []) for id in ids])

        value_obj = self.pool.get('value')
        value_ids = value_obj.search(cr, uid, vals = [('property_id', '=', property_id)])
        return dict([(id, value_ids) for id in ids])

 

       _columns = {       

                'value_ids': fields.function(get_value_ids, type='one2many',
                                                                                                   relation='value'),

        }

        _defaults = {

                'value_ids': lambda self, cr, uid, context : self.get_value_ids(cr, uid, [0], '', '', context)[0],

        }

The trick is to use a lambda function and pass a fake ids list to match de function implementation.

0
Ivan
On 8/27/14, 8:13 AM

get_value_ids method should handle multiple ids.  So it should be something like:

def get_value_ids(self, cr, uid, ids, field_name, arg, context=None):
                property_id = context.get('property_id', False)
                value_ids = []

                if property_id:
                        value_obj = self.pool.get('value')
                        value_ids = value_obj.search(cr, uid, vals = [('property_id', '=', property_id)]
                return dict([(x, value_ids) for x in ids])

Yep, you're totally right. One more time, I was too fixed on my use case ! I just improved my code to handle multiple ids (even if it should never happen).

Cyril Sester
on 8/27/14, 8:27 AM
-1

Hi,

You have to just change the defination of your fields as like below.

Class property(orm.Model):

        _name = 'property'

        _columns = {

                'value_ids': fields.many2many(

                        'value', 'property_value', 'property_id', 'value_id', _('Values'))

        }

Class value(orm.Model):

        _name = 'value'

        _columns = {

                'property_ids': fields.many2many(

                        'property', 'property_value', 'value_id',  'property_id',_('Properties')),

You have to just redefine the column of the 'value' model. In that you have to specify the first column for the 'value' model and second column for the 'property' model. 

I think this is the first thing you have to correct.

Just a small mistake who went in when writing the simplified relationship. Was right in the code. Everything works fine, from properties I see associated values and from values associated properties. My problem is only in the wizard where the fields.function isn't computed on wizard creation.

Cyril Sester
on 8/25/14, 10:41 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 8/25/14, 10:28 AM
Seen: 6416 times
Last updated: 3/16/15, 8:10 AM