Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
14191 Lượt xem

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,

 

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

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.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 24
442
1
thg 7 18
3399
1
thg 2 16
4942
0
thg 1 16
3455
1
thg 9 15
5185