I have tried to display name , description fields for the many to one field. The code is as follows, help me to resolve this problem
from openerp.osv import orm, fields
class post_branch(orm.Model):
_name = "branches"
_columns = {
'name':fields.char("Name", size=50, required=True),
'description':fields.text("Description",),
'emp_id':fields.one2many("man", "branch_id", "lines")
# 'emp_id':fields.many2one("man","lines",required=True)
}
def name_get(self, cr, uid, ids, context=None):
res = []
for r in self.read(cr, uid, ids['name', 'description']):
res.append(r['id'], '%s,%s' (r['name'],r['description']))
return res
# def name_des(self, cr,uid, context=None):
# obj_name=self.pool.get('')
Try following,
def name_get(self, cr, uid, ids, context=None):
res = []
if not ids:
return res
for r in self.browse(cr, uid, ids, context=context):
name = str(r.name) + ',' + str(r.description or '')
res.append((r.id, name))
return res
Related
how can I use many2one field in the function?
this is my code:
def _get_unit(self, cr, uid, ids, fields,arg, context=None):
res = {}
list_data = []
for record in self.browse(cr, uid, ids,unit):
list_data.append(record[unit.id])
return super(learning_course, self)._get_unit(cr, uid, ids, context=context)
_columns = {
'unit': fields.many2one('hr.department', 'unit'),
'department': fields.function(_get_unit, string='department' , store=True ,type='many2one' ,relation='hr.department'),
}
def onchange_user(self, cr, uid, ids, user_id, context=None):
unit = False
if user_id:
unit = self.pool.get('res.users').browse(cr, uid, user_id, context=context).context_department_id.id
return {'value': {'unit' : unit }}
return {'value': {} }
but I get this error:
for record in self.browse(cr, uid, ids,unit):
AttributeError: 'browse_record_list' object has no attribute 'id'
what should I do?
def _get_unit(self, cr, uid, ids, prop, unknow_none, context=None):
res = {}
for record in self.browse(cr, uid, ids):
res [record.id] = record.user_id.context_department_id.id
return res
_columns = {
'user_id': fields.many2one('res.users', 'user', readonly=True),
'unit_id': fields.function(_get_unit, string='dep' , store=True ,type='many2one',relation='hr.department'),
}
I have a relation between the fuel and voucher classes with the field 'amount_used' that calculates the number of voucher x used in fuel without problems
_name = 'fleet.vehicle.log.fuel'
'voucher_id': fields.many2one('fleet.voucher', 'Voucher'),
_name = 'fleet.voucher'
'amount_used': fields.function(_count_all, type='integer', string="Amount Used")
def _count_all(self, cr, uid, ids, field_name, arg, context=None):
Fuel = self.pool['fleet.vehicle.log.fuel']
return {
voucher_id: Fuel.search_count(cr, uid, [('voucher_id', '=', voucher_id)], context=context)
for voucher_id in ids
}
when I add the store parameter in 'amount_used' I have an update problem
store=True or store={'fleet.vehicle.log.fuel': (lambda self, cr, uid, ids, c={}: ids, ['voucher_id'], 10)}
store={'fleet.vehicle.log.fuel': (_get_voucher, ['voucher_id'], 10)}
def _get_voucher(self, cr, uid, ids, context=None):
res = []
for fuel in self.pool.get('fleet.vehicle.log.fuel').browse(cr, uid, ids, context=context):
if fuel.voucher_id:
res.append(fuel.voucher_id.id)
return res
I have a problem since a month of updating the function field 'result' that uses a different field function 'field2' (updated without problems) ?
_name = 'x'
'field1': fields.integer(readonly=True),
'field2': fields.function(count_x_id_in_y, type='integer',
store={'y': (get_x_id_from_y, ['x_id'], 10)}),
'result': fields.function(add, type='integer',
store={'x': (lambda self, cr, uid, ids, c={}: ids, ['field1','field2'], 10)}),
def add(self, cr, uid, ids, field_name, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
res[record.id] = record.field1 + record.field2
return res
Created a column 'line_no' when i updated the value it throws the KeyError: 40
My code:
def _get_line_no(self, cr, uid, ids, line_no, arg, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
nextno =0
no = record.next_line_no
next_no = nextno + no
total =+ next_no
res={
'next_line_no':next_no,
'line_no': total
}
return res
_columns = {
'line_no':fields.function(_get_line_no,string='Line No',type='integer'),
'next_line_no':fields.integer(' Next Line No'),
}
_defaults = {
'next_line_no':1
}
Throws Error : KeyError: 40
How do i resolve?
next_line_no is a database field, So it won't effect the dynamic way.
You need to modify this way,
def _get_line_no(self, cr, uid, ids, line_no, arg, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
nextno =0
no = record.next_line_no
next_no = nextno + no
total += next_no
res[record.id]={
'next_line_no':next_no,
'line_no': total
}
return res
_columns = {
'line_no':fields.function(_get_line_no,string='Line No',type='integer', multi="lineno"),
'next_line_no': function(_get_line_no,string='Next Line No',type='integer', multi="lineno", store=True),
}
_defaults = {
'next_line_no':1
}
I hope this will yelp you.
Change the columns by,
_columns = {
'line_no':fields.function(_get_line_no,string='Line No',type='integer', multi="line"),
'next_line_no':fields.function(_get_line_no, type='integer', string='next line number' ,multi="line"),
}
and your method like,
def _get_line_no(self, cr, uid, ids, field_names, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
nextno =0
no = record.next_line_no
next_no = nextno + no
total += next_no
res[record.id]={
'next_line_no':next_no,
'line_no': total
}
return res
This will work.
in ticket.py. I have two class. class deposit.line and res_partner (inherit). I want to take the date of the class deposit.line but its function in the class res_partner(inherit)
def _compute_dept2(self, cr, uid, ids, amount, arg, context=None):
result = {}
obj2 = self.pool.get('deposit.line')
for record in obj2.deposit_line:
temp1 = record.date
print temp1
print result
return result
but the results of its existing print false. what wrong ? please correct my code
PS:
My explanation is less good. but look at my code,surely knowing my explanation.
THIS MY COMPLETE CODE:
class deposit_line(osv.osv):
_name ="deposit.line"
_description = "Deposit Line"
_columns = {
'name': fields.char('Name', size=64),
'ref': fields.char('Reference', size=64),
'amount': fields.float('Amount'),
'date': fields.date('Date', required=True),
'deposit_id': fields.many2one('res.partner', 'Deposit ', required=True, ondelete='cascade'),
}
deposit_line()
class res_partner(osv.osv):
_inherit = 'res.partner'
def _compute_age(self, cr, uid, ids,date_birth,age,arg, context=None):
result = {}
for r in self.browse(cr, uid, ids, context=context):
age=0
if r.date_birth:
age = (datetime.now()-datetime.strptime(r.date_birth,"%Y-%m-%d")).days/365.25
result[r.id] = age
return result
def _compute_dept(self, cr, uid, ids, deposit, available, arg, context=None):
result = {}
for r in self.browse(cr, uid, ids, context=context):
avail=0
temp = r.available
if r.deposit:
avail = r.deposit + temp
result[r.id] = avail
return result
def _compute_dept2(self, cr, uid, ids, amount, arg, context=None):
result = {}
obj2 = self.pool.get('deposit.line')
for record in obj2.deposit_line:
temp1 = record.date
print temp1
print result
return result
_columns = {
'speaker': fields.boolean('Leader'),
'event_ids': fields.one2many('event.event','main_speaker_id', readonly=True),
'event_registration_ids': fields.one2many('event.registration','partner_id', readonly=True),
'airline': fields.boolean('Airlines'),
'hotel': fields.boolean('Hotel'),
'date_birth': fields.date('Date of Birth'),
'id_no': fields.char('ID. No', size=20),
'id_expired': fields.date('Expired Date'),
'sex':fields.selection([('male','Male'),('female','Female')],'Sex'),
'age' : fields.function(_compute_age, type='float', method=True, store=True, string='Age', readonly=True),
'deposit': fields.function(_compute_dept2, type='float', method=True, store=True, string='Deposit', readonly=True),
'available': fields.function(_compute_dept, type='float', method=True, store=True, string='Available', readonly=True),
'deposit_ids':fields.one2many('deposit.line', 'deposit_id', 'Deposit Line'),
}
res_partner()
Since you have one2many field for deposit_line defined in the res.partner model, you do not need the to access deposit_line object directly.
def _compute_dept2(self, cr, uid, ids, amount, arg, context=None):
result = {}
for partner in self.browse(cr, uid, id, context=context)
result[partner.id]=0
for deposit_line in partner.deposit_ids:
result[partner.id] += deposit_line.amount
return result
After line 3, you forgot to:
obj2.browse(cr, uid, ids, context=context)
You should learn to use the debugger:
Add the line import pdb; pdb.set_trace() where you want to place a breakpoint. When the Python reaches the breakpoint it stops at the console with a (pdb) prompt. There type p obj to print variable obj, n to step to the next instruction, and hfor help. You might find more info in this post and in the docs.