Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
14196 Widoki

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,

 

Awatar
Odrzuć
Autor Najlepsza odpowiedź

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.

Awatar
Odrzuć
Najlepsza odpowiedź

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])

Awatar
Odrzuć
Autor

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).

Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor

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.

Powiązane posty Odpowiedzi Widoki Czynność
1
cze 24
442
1
lip 18
3400
1
lut 16
4944
0
sty 16
3455
1
wrz 15
5186