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

Fields.function with type one2many in wizard

By
Cyril Sester
on 8/25/14, 10:28 AM 5,072 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

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
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

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 8/25/14, 10:34 AM

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

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

1 follower(s)

Stats

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