I am learning to develop OpenERP modules, and one of the things I need to do is calculate the average of all the inputs from the user.
My idea was to loop the records while keeping the sum and the count and then make the average, but I can't seem to understand how to access the value for the total field for each record in the sim.students table
Here is part of my code
def get_text(self, cr, uid, ids, fields, arg, context):
result = {}
i = 0
for id in ids:
print self.browse(cr,uid,id,['total'])
print id
i = i+1
print i
return result
But the result of printing self.browse(cr,uid,id,['total']) returns me browse_record(sim.student, 3)and not the total itself.
I know this must be really simple but I can't seem to figure out how to get to that value.
Any tip much appreciated
So this is what I got from here:
browse(cr ,uid, select, context=None, list_process=None, fields_process=None)
where:
cr = database cursor
uid = user id
select = id or list of ids
context = context arguments like lang, time zone
It returns an object with all the fields accessible by dot notation. So you can do something like:
records = self.browse(cr, uid, ids)
for rec in records:
print rec.total
print rec.otherfield
or if you like list comprehensions:
records = self.browse(cr, uid, ids)
totals = [rec.total for rec in records]
average = sum(totals)/len(totals)
Related
I am trying to find same object in Odoo using search. I get back nothing or just "[]" from my search.
This is my code:
values = []
get_own_data = self.browse(cr, uid, ids)
for get_own in get_own_data:
get_date = self.pool.get('account.move').search(cr, uid, [('date', 'like', get_new.year)])
if get_date:
for new_data in get_date:
datas = self.pool.get('account.move').browse(cr, uid, new_data)
for data in datas.line_id:
product_categ = self.pool.get('product.category').search(cr, uid, [("property_account_income_categ", "=", data.account_id)])
print product_categ
print "================"
I don't understand why I got nothing. The result doesn't give me good information.
I need to do two activities in my activity method.
one is need to return value for total_workers field.but need to return ID also because its necessary requirement in OpenERP when we override that method.
then have to return 2 things.please advice me how to implement this in my form.
with one return value this is worked for both things.but need to return both.
def create(self, cr, uid, values, context=None):
name = 'CheckRoll No = ' + str(values['checkroll_no']) + ' & Gang No = ' + str(values['gang_no'])
values.update({'name': name})
total_tea_workers = 0
offer_id = super(bpl_work_offer, self).create(cr, uid, values, context=context)
tea_worker_obj = self.browse(cr, uid, offer_id, context=context) or []
tea_worker_ids = self.pool.get('bpl.selected.tea.workers.line').search(cr, uid, [('tea_line_worker_id', '=', tea_worker_obj.id)])
for tea_worker in self.pool.get('bpl.selected.tea.workers.line').browse(cr, uid, tea_worker_ids):
if tea_worker.is_selected:
total_tea_workers += 1
return {'value': {'total_workers': total_tea_workers}}
return offer_id
EDITED
I sort it out in this way.hope this will help for others
:-)
i wrote a function for my field
def _compute_workers(self, cr, uid, ids, fieldname, arg, context=None):
total_workers = total_tea_workers = total_rubber_workers = total_sundry_workers = total_other_workers = 0
res = dict.fromkeys(ids, False)
for this in self.browse(cr, uid, ids, context=context):
tea_worker_ids = self.pool.get('bpl.selected.tea.workers.line').search(cr, uid, [('tea_line_worker_id', '=', this.id)])
for tea_worker in self.pool.get('bpl.selected.tea.workers.line').browse(cr, uid, tea_worker_ids):
if tea_worker.is_selected:
total_tea_workers += 1
rubber_worker_ids = self.pool.get('bpl.selected.rubber.workers.line').search(cr, uid, [('rubber_line_worker_id', '=', this.id)])
for rubber_worker in self.pool.get('bpl.selected.rubber.workers.line').browse(cr, uid, rubber_worker_ids):
if rubber_worker.is_selected:
total_rubber_workers += 1
sundry_worker_ids = self.pool.get('bpl.selected.sundry.workers.line').search(cr, uid, [('sundry_line_worker_id', '=', this.id)])
for sundry_worker in self.pool.get('bpl.selected.sundry.workers.line').browse(cr, uid, sundry_worker_ids):
if sundry_worker.is_selected:
total_sundry_workers += 1
other_worker_ids = self.pool.get('bpl.selected.other.workers.line').search(cr, uid, [('other_line_worker_id', '=', this.id)])
for other_worker in self.pool.get('bpl.selected.other.workers.line').browse(cr, uid, other_worker_ids):
if other_worker.is_selected:
total_other_workers += 1
total_workers = total_tea_workers + total_rubber_workers + total_sundry_workers + total_other_workers
res[this.id] = total_workers
return res
i changed my integer field to functional field
'total_workers': fields.function(_compute_workers, type="integer", string="Total Workers"),
You should never return anything else in create() except the ID of the record that was created. Usually this is done by returning the result of the call to the parent via super(myclass, self).create(...) indeed.
It's not clear what you are trying to achieve with your total_workers code. If total_workers is supposed to be a computed field, you don't have to override create at all: just declare this column as a fields.function and put the code to compute it in the corresponding function.
For example:
def _compute_workers(self, cr, uid, ids, fieldname, arg, context=None):
result = dict.fromkeys(ids, False) # default result for each id
for this in self.browse(cr, uid, ids, context=context):
# let's assume you have a one2many called worker_ids
result[this.id] = len(this.worker_ids)
return result
_columns = {
'total_workers': fields.function(_compute_workers, type="integer",
string="Total Workers")
}
At this point it would be a good idea to have a good look at the OpenERP technical memento and the OpenERP framework documentation, to get an overview of the OpenERP API and how it is supposed to be used :-)
Once you have a good understanding of the structure of models, the various field types, the CRUD methods and the inheritance patterns, you should be able to quickly browse the source code of the official modules and find examples of anything you want to accomplish. This should make your life much more easier!
I think that you're doing the counting of the workers in a very inefficient manner.
You're doing your SQL filter to obtain each of the workers matching an ID, and then testing for a single flag within them.
I think it would be much more efficient to just add another criteria to your select statement.
total_tea_workers = self.pool.get('bpl.selected.tea.workers.line').search(cr, uid, [('tea_line_worker_id', '=', id), ('is_selected', '=', True)]), count=True)
In Openerp, we have object_A with one one2many field belonging to object_B. Object_B has a float field. In object_A we have a one2many_list widget for the corresponding object_B so naturally we'd have multiple rows for each new record.
I know it's trivial but I'm having a hard time writing a function in object_A to sum up the total value of Object_B float column. What i have so far is something like that:
def get_result(self, cr, uid, ids):
total = {}
for obj in self.browse(cr, uid, ids):
result=0.0
total[result]+= obj.o2m_field.float_field
return total
The code provided by #DReispt should work, and if you approve an answer, please approve his, and not mine.
The important thing to understand is that a function field in OpenERP returns a dictionary with the object ids for the key and the value of the field for the given object as the associated value.
In your orginal code:
result = 0.0
total[result] += anything
would result in a KeyError since the dictionary is empty originally (total = {} at the beginning of your code).
A shorter version of DReispt code would be
def get_result(self, cr, uid, ids, context=None):
total = {}
for obj in self.browse(cr, uid, ids, context=context):
total[obj.id] = sum(o2m.float_field for o2m in obj.o2m_field)
return total
This version takes advantage of Python's generator expression which can be passed to the sum() built in function. It is also slightly faster, because you avoid accessing the total dictionary multiple times for each object.
You need to loop the o2m:
def get_result(self, cr, uid, ids, context=None):
total = {}
for obj in self.browse(cr, uid, ids, context=context):
total[obj.id] = 0
for o2m in obj.o2m_field:
total[obj.id] += o2m.float_field
return total
#example: {56: 12.34, 57: 56.78}
Just for kicks, doing it the functional way:
def get_result(self, cr, uid, ids, context=None):
return {obj.id: sum(o2m.float_field for o2m in obj.o2m_field)
for obj in self.browse(cr, uid, ids, context=context)}
I want to calculate the value of a function field using its previous value ( = value of the record with previous id)
'testrest' : fields.function(get_reste, method=True, string='Restant',type='integer'),
def get_reste(self, cr, uid, ids, field_name, arg, context):
x = {}
for record in self.browse(cr, uid, ids ,context):
if record.statut != 'entree':
x[record.id]= a + record.entree_nbr # a should be the same field for the previous record
How can I do that? thank you
First point here about OE 6.1+ and fields.function() : it does not take a method parameter anymore [ Server rev 3495 revid odo#openerp.com-20110701232328-flgxulxva70vnyxr and addons rev 4844].So please do not use the "method" parameter anymore!
Now you want to calculate the value based on previous value so what you can do is you can use store=True param here that will store your previous value in data now in your calculation for your record you can read previous value and calculate new value and return it.
'testrest' : fields.function(get_reste, store=True, string='Restant',type='integer'),
def get_reste(self, cr, uid, ids, field_name, arg, context):
x = {}
for record in self.browse(cr, uid, ids ,context):
if record.statut != 'entree':
x[record.id]= record.testrest + record.entree_nbr
return x
Here benefit of string will be you can use this value any where out side OE or fro some external reporting tool and you can even expoer this field.
Hope this will help.
Some More Code :
'price': fields.function(_price_get, method=True, string="Price", store=True),
def get_reste(self, cr, uid, ids, field_name, arg, context):
x = {}
a = 0.0
for record in self.browse(cr, uid, ids ,context):
if record.statut != 'entree':
x[record.id]= a + record.entree_nbr
a =record.testrest
return x
If you need you can sort the list of ids by ids.sort()
I do a project about Timesheet in OpenERP. I have this problem:
this is x_luong table.
class x_luong(osv.osv):
_name = 'x_luong'
_description = 'Luong'
_columns = {'name': fields.many2one('x_nhanvien', 'Mã nhân viên', size=10, required='1'),
'ma_luong': fields.integer('ma luong', size=10, required='1'),
'giolam': fields.float('Giờ làm', size=100, required='1'),
'giolamthuc': fields.char('Gio lam thuc te', size=5, required='1'),
'time_in': fields.char('Gio vào', size=20),
'time_out' :fields.char('Gio về', size=20),
'state' :fields.selection([('dangnhap','Đẳng nhập.'),('rave','Ra về')]),
'test': fields.integer('Kiem tra', size=20),
'phutvao': fields.integer('Phut vao ', size=20),
'phutra': fields.integer('phut ra', size=20),
}
_defaults = {'state':'dangnhap',
}
and this some function in it:
this 2 function mean get time when the staff sign_in or sign_out the system:
def get_timein(self,cr,uid,ids,context={}):
obj = self.browse(cr,uid,ids,context=context)[0]
timein = str(datetime.now())
self.write(cr, uid, ids, {'time_in':timein }, context=context)
return 1
def get_timeout(self,cr,uid,ids,context={}):
obj = self.browse(cr,uid,ids,context=context)[0]
timeout = str(datetime.now())
self.write(cr, uid, ids, {'time_out':timeout }, context=context)
return 1
and this 2 function for button sign_in and sign_out:
def cho_dangnhap(self,cr,uid,ids,context={}):
self.pool.get('x_luong').write(cr,uid,ids,{'state':'dangnhap'})
self.get_timein(cr,uid,ids)
return 1
def cho_rave(self,cr,uid,ids,context={}):
self.pool.get('x_luong').write(cr,uid,ids,{'state':'rave'})
self.get_timeout(cr,uid,ids)
self.tinh_thoigian(cr,uid,ids)
self.insert(cr,uid,ids)
function tinh_thoigian mean cut the string time for get ... hour or min for calculation
def _thoigianlam(self,cr,uid,ids,context={}):
obj = self.browse(cr,uid,ids,context=context)[0]
hour_den = int(obj.time_in[12:13])
hour_di = int(obj.time_out[12:13])
min_den = int(obj.time_in[15:16])
min_di = int(obj.time_out[15:16])
gl = int(hour_di)-int(hour_den)
pl = min_di-min_den
thucte = str(gl)+':'+pl
self.write(cr, uid, ids, {'giolam':gl }, context=context)
self.write(cr, uid, ids, {'giolamthuc':thucte }, context=context)
return 1
and last function insert() get ma_luong(i think this same the primary key in sql) and giolam(the hour of the staff work in company), time_in, time_out and this is function insert()
def insert(self,cr,uid,ids,context={}):
obj = self.browse(cr,uid,ids,context=context)
values = {'ma_luong':obj.name.id,
'giolam':obj.giolam,
'time_in':time_in,
'time_out':time_out,
self.pool.get('x_giolam').create(cr,uid,values,context=context)
with this function i want insert data in table x_giolam because when the staff sign in or sign out the system in day ... the data of it with save in this table and a other day when they do it again it with save it again ... and last month if you want calculation about salary of them you just select ma_luong=ma_luong(of table x_luong) and this table x_giolam:
class x_giolam(osv.osv):
_name = 'x_giolam'
_description = 'Gio Lam'
_columns = {'name': fields.integer('Lọai',size=64,required="true"),
'giolam' : fields.float('Gio lam',size=64,required="True"),
'time_in': fields.char('Gio vào',size=20),
'time_out' :fields.char('Gio về',size=20),
}
and i have 3 question with my project:
1) function insert have aerror:
AttributeError: 'browse_record_list' object has no attribute 'name'
How can i fix it ??? i data of it is save in table x_giolam
2) how can i select many row of table x_giolam which of thte employee' own.. give me some example about this function
3) how i can organization field.Xml when i show rows in
Sorry for your troubles because it is so long ... but i hope every body in here can help me. Python and open Erp so difference with c++ or c#. And this my project"research and write a module timesheet with OpenErp" of me and next week is deadline.
English of me not good, i'm sory about it!!!
Thanks!!
I can help with your first question. The problem is in this code:
def insert(self,cr,uid,ids,context={}):
obj=self.browse(cr,uid,ids,context=context)
values={'ma_luong':obj.name.id,
The error message was like this:
AttributeError: 'browse_record_list' object has no attribute 'name'
If you call orm.browse() with a list of ids, you will get back a list of browse records. You then have to enumerate through the list, or get a single entry from the list to work with.
For example:
for luong in self.browse(cr,uid,ids,context=context):
print luong.name
Or:
luongs = self.browse(cr,uid,ids,context=context)
luong = luongs[0]
print luong.name
Why don't you take a look at the standard hr_attendance module and go on from there?
For your model, the name is a reserved field name, so it would be best to keep it as achar. Try that change and see if it solves your error message.
For the other two questions, I think you should try to rephrase them a little better...
The type of obj is list of records, so for browse the list of records, you must define a one element.
in your case, you can type : obj[0].giolam --> for the giolam of the first record of obj.
forgive me for my bad english