It is currently Mon May 21, 2012 6:58 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: [SOLVED] How to manipulate on_change or object methods?
PostPosted: Fri Jun 03, 2011 1:24 pm 
Offline

Joined: Fri Jun 03, 2011 12:14 am
Posts: 32
Hello,

I'm going to manipulate the product_id_change() method of class sale_order_line(osv.osv): and add a new argument. (product_id_change is used as on_change property for product_id in the sale.order.form view.)

In general my question is: How should I manipulate method x() of an object.

I'm thinking of:
  • Create a new Module
  • Implement an Adapter/Wrapper by inheriting the object and overwriting the method by adding a new argument.
  • Call the method of the new object in on_change like myObj.product_id_change(...)

Is this the best practise in OpenERP or what would you recommend?


Last edited by hever on Wed Jun 08, 2011 10:50 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Fri Jun 03, 2011 1:32 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
I think that is always better to inherit from standard classes, so that when you update module it doesn't got overwritten. As you want to change arguments, you have to inherit view to modify on_change field, and point to your new method. It's also advisable to call with super the other method and process return value not to break functionality with updates (for example, adding changed values to your return dictionary).

Regards.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Fri Jun 03, 2011 1:55 pm 
Offline

Joined: Fri Jun 03, 2011 12:14 am
Posts: 32
I'm not going to change the on_change property I'm going to change product_id_change().

And if I rethink of it I don't need to add a new argument I just have to return another value in the return dict. (I have to update a field (my own item number) in the view if a product is choosen.)

I thought about inerhiting from sale_order_line and overwriting product_id_change. In there of course I'm going to call super and add then my own field to the return dict.

So because I'm not going to change the view you agree in inheriting sale_order_line? Because I've to overwrite product_id_change.

Thank you!


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Fri Jun 03, 2011 2:13 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
OK then, you only need to inherit sale_orde_line and overwrite the method, taking in account what we said previously.

Good coding!
Regards.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Fri Jun 03, 2011 4:20 pm 
Offline

Joined: Tue Mar 29, 2011 5:14 pm
Posts: 14
Hello everyone, i've a problem " how to fill a field automatically ??"
i have three fields Article, Lot and Market in my class , i want that the two fields Lot and Market will be filled automatically when i choose the Article (exactly like when we choose a partner ,his adress is filled automatically).

here is my class that adds those fields to the purchase order line :

Code:
class purchase_order_line(osv.osv):
        _name = "purchase.order.line"
        _inherit = "purchase.order.line"
   _columns = {
         'id_article': fields.many2one('market.articles' , 'Article', required=False),
    'lot_id' : fields.many2one('market.lots', 'Lot Name'),
    'market_id' : fields.many2one('market.articles', 'Market Name'),
     }


and the code of my class Market look like this :
Code:
class market_markets(osv.osv):
   _name = "market.markets"
   _columns = {
      'name' : fields.char('Market Name', size=30, required=True),
      'lot_ids' : fields.one2many('market.lots', 'market_id', 'lot Name'),
              }
market_markets()

class market_lots(osv.osv):
   _name = "market.lots"
   _columns = {
             'name' : fields.char('Lot Name', size=30),
             'article_ids' : fields.one2many('market.articles', 'lot_id', 'Article Name'),
                       }
market_lots()

class market_articles(osv.osv):
      _name = "market.articles"
      _columns = {
      'name' : fields.char('Article Name', size=30, required=True),
               }
market_articles()



Please , any ideas ???????


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Fri Jun 03, 2011 4:40 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
Soukaina, I think that is not a good idea to spam topics with your question when you have posted it in your own topic. I'll try to answer you, but not here.

Regards.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Sun Jun 05, 2011 12:49 pm 
Offline

Joined: Tue Mar 29, 2011 5:14 pm
Posts: 14
ok thanks Pedro :)


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Mon Jun 06, 2011 7:57 pm 
Offline

Joined: Fri Jun 03, 2011 12:14 am
Posts: 32
Hello,

I tried to overwrite product_id_change in sale_order_line:
Code:
from osv import osv, fields

class sale_order_line(osv.osv):
  _name = 'sale.order.line'
  _inherit = 'sale.order.line'
  _columns = {
  }
  _defaults = {
  }

  def product_id_change( self, cr, uid, ids, pricelist, product, qty=0,
      uom=False, qty_uos=0, uos=False, name='', partner_id=False, lang=False,
      update_tax=True, date_order=False, packaging=False, fiscal_position=False,
      flag=False ):
    return {'value': {'th_weight': 0, 'product_packaging': False,
        'product_uos_qty': qty, 'x_code': '123'}, 'domain': {'product_uom': [],
           'product_uos': []}}
#    return super(C, self).product_id_change( self, cr, uid, ids, pricelist,
#        product, qty=0, uom=False, qty_uos=0, uos=False, name='',
#        partner_id=False, lang=False, update_tax=True, date_order=False,
#        packaging=False, fiscal_position=False, flag=False)
sale_order_line()


As you see I'm going to return 'x_code': '123'. But if I add a new product in the sale.order.form the x_code field doesn't change and the other fields are still updated. So nothing changed. What did I miss? Must the product_id_change() method be called somehow special after it's overwritten?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Mon Jun 06, 2011 8:12 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
Hever, the returned values in on_change methods are not written in database, only put in the corresponding fields in the form, so maybe you don't have the field 'x_code' in screen?

Regards.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Mon Jun 06, 2011 9:06 pm 
Offline

Joined: Fri Jun 03, 2011 12:14 am
Posts: 32
It is in the view but it gets not updated. And the other fields are still updated, but they shouldn't.

And perhaps I should mention this class sale_order_line is in my own module.

Perhhaps I reinstalled / upgraded the module wrong after changes. I removed it an then I reinstalled it but I did not remove the files in the filesystem.

But basically my code is correct?


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Mon Jun 06, 2011 9:26 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
This is probably due to the tricky update process. I recommend to install module in a fresh database to be sure.

I don't find any noticeable bug in your code, but it is always advisable to call super method and process its result (for example, adding x_code dictionary entry), so that you don't supress standar behaviour.

Regards.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Mon Jun 06, 2011 9:33 pm 
Offline

Joined: Fri Jun 03, 2011 12:14 am
Posts: 32
How do you develop modules? I mean how to you validate your changes live without a big reinstallation routine? It's very ineffective to remove and reinstall a module with every change.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Mon Jun 06, 2011 9:40 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
Yeah, I also don't find the most accurate process, but these are the usual steps I follow:
  • If I only change view things (XML files), I go to Modules and click on Schedule Upgrade.
  • For python code changes, I start server from command line with --update=<module_name> switch.
  • For CSV or XML imports (in the section init_files), I start server with --init=<module_name>.

But sometimes, I don't get things updated and I don't know why!! It's a bit frustating!!

Regards.


Top
 Profile  
 
 Post subject: Re: How to manipulate on_change or object methods at all?
PostPosted: Wed Jun 08, 2011 10:49 am 
Offline

Joined: Fri Jun 03, 2011 12:14 am
Posts: 32
Thanks pedro, the --update=<module_name> switch is very helpful!

I update my fields now using the following code:
Code:
from osv import osv, fields

# http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/object_inherit.html
class sale_order_line(osv.osv):
    _name = 'sale.order.line'
    _inherit = 'sale.order.line'
   
    # http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/field_type.html
    _columns = {
        'item_no': fields.char('Item No.', size=15, required=True),
    }

    # http://doc.openerp.com/v6.0/developer/2_6_views_events/events/events.html#on-change
    # http://www.openerp.com/forum/topic25524.html
    def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
            lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False):
       
        result = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
                uom, qty_uos, uos, name, partner_id, lang, update_tax, date_order, packaging, fiscal_position, flag)
       
       
        # http://www.openerp.com/forum/topic24670.html
        product_obj = self.pool.get('product.product')
        reads = product_obj.read(cr, uid, product)
        result['value'].update({'item_no': reads['item_no']})
       
        return result
   
sale_order_line()


I found an example about accessing the columns in the forum (Problem accessing field data many2one same class object) but it would be nice if somebody could point me to the official documentation (if it exists).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP