It is currently Mon May 21, 2012 7:02 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 11:41 am 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
Hi, I'm newbie to openerp and I am trying to develop a simple module for my company.

I try to understand the usage of function fields http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/field_type.html and ORM Methods http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html to make my module more useful.

In the beginning I try to make some calculation of fields of my object and store them to a new field.
So in my class add the field
Code:
'cost_sum': fields.function(total_project_cost, method = True, type = 'integer', String='Sum cost')


and the definition of the function that make the calculation(the below example is the simplest that I try)
Code:
def total_project_cost(self, cr, uid, ids, fields, arg, context=None):
   x={}
   x[0] = 5
   return x


So, as I read at the above links I thing that the field cost_sum will be filled with the value 5.
But maybe I try something wrong.

Is there any simply tutorial how to use function fields. In developer manual is complicated I thing. Some example will be useful.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 11:50 am 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
All you are doing is correct except the key value for dictionary element. You have to put record id instead of 0:

Code:
def total_project_cost(self, cr, uid, ids, fields, arg, context=None):
   x={}
   for record in self.browse(cr, uid, ids):
      x[record.id] = 5
   return x


This is a mechanism to fill multiple records with same method in case you area doing a massive change.

Regards.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 12:31 pm 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
Thanks for your help.
I try it but maybe I'm doing something wrong. I attach the whole code of my class.
Code:
class easypms_project(osv.osv):

   def total_project_cost(self, cr, uid, ids, fields, arg, context=None):
      x={}
      for record in self.browse(cr, uid, ids):
         x[record.id] = 5
      return x
   
   _name = 'easypms.project'
   _columns = {
      'name': fields.char('Project Name', size=255),
      'category':fields.many2one('easypms.project_category', 'Κατηγορία'),
      'cost': fields.float('Συμφωνημένο Κόστος',digits=(12,2)),
      'cost_sum': fields.function(total_project_cost, method = True, type = 'integer', String='Συνολικό Κόστος Project')
      'customer_id': fields.many2one('res.partner', 'Πελάτης'),
      'task_of_project': fields.one2many('easypms.task','project_id','Λίστα task του project'),
      'renewal_of_project': fields.one2many('easypms.renewal','project_id','Λίστα ανανεώσεων του project'),
      'date_start': fields.datetime('Ημερομηνία Έναρξης'),
      'date_finish': fields.datetime('Ημερομηνία Παράδοσης'),
      'notes': fields.text('Περιγραφή'),
      'status': fields.many2one('easypms.status', 'Κατάσταση'),
   }
easypms_project()

Please ignore any other field and help me to resolve the problem of functional field.

When I try to save these changes and try to login to openerp, the system raise an error that the user or password is incorrect. But I'm sure that is correct because when I comment the code with the function, I can login.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 12:41 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
Usually, the error that OpenERP raises is "Database cannot be accessed", but the message you mentioned is that indeed you don't put correct credentials. I have checked the code and everything seems correct, altough maybe you are going to have problems with string parameters, because you are using greek characters. You can make two things:
  • Use ustr('unicode_string') function to assure is a unicode string.
  • Use english and then translate. It's a bit slower at first, but it's more correct.

Try again and tell me.

Regards.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 1:22 pm 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
I try to change the string from Greek letters to English, restart db-server but it still raise the same error.
But when I comment the code of function all works fine. Still with Greek letters in strings.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 1:29 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
Can you paste server log here so that I can figure out what is the problem? In openerp-server.conf, there is one section where you can setup this option.

Regards.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 1:39 pm 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
Sorry is my fault in syntax.
Code:
'cost_sum': fields.function(total_project_cost, method = True, type = 'integer', String='Συνολικό Κόστος Project')
need a , char at the end.

I try to enable server log for feuture usage.

Thanks.

No I try to find usefull info about extended usage of function nad ORM methods. Is any other doc that explain the use of that resources?

Thanks Again.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 2:05 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
Yeah, of course, it's so obvious that any of both fell in that.

I'm afraid there is no more documentation (or I don't know it), except simple tutorials over the net covering certain topics.

Regards.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 2:51 pm 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
How can I debug some objects? Is there any auto class like var_dump in php.
I try pprint but I don't khow how to import it and use it.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Thu Jun 16, 2011 3:51 pm 
Offline

Joined: Thu May 26, 2011 8:51 pm
Posts: 164
See this topic for that:

http://www.openerp.com/forum/topic6334.html

Regards.


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Fri Jun 17, 2011 2:34 pm 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
OK thans for help, I 'll try it.
But it seems so complicated. There is no other dump simple function?


Top
 Profile  
 
 Post subject: Re: Simple use of function type field and ORM methods
PostPosted: Sat Jun 18, 2011 11:44 am 
Offline

Joined: Thu Mar 10, 2011 4:30 pm
Posts: 145
Hi,
I found very useful the below link
http://www.slideshare.net/openobject/openerp-technical-memento


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 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