AttributeError: 'model.name' object has no attribute 'generate' - odoo

I'm trying to call a function from a wizard button but I get this error:
AttributeError: 'model.name' object has no attribute 'generate'
AttributeError: 'model.name' object has no attribute 'generate'
this is my model code:
class wizard(models.TransientModel):
_name = 'model.name'
department_id = fields.Many2one('hr.department')
employee_id = fields.Many2one('hr.employee')
date_from = fields.Date(string = 'Start Date', required = True)
date_to = fields.Date(string = 'End Date', required = True)
state = fields.Selection([('draft', 'Draft'), ('verify', 'Waiting'),('done', 'Done'),('cancel', 'Rejected'),])
def generate(self, cr, uid, ids, context=None):
return self.write(cr, uid, ids, {'state': 'draft'}, context=context)
In the xml file:
<button name ="generate" type="object" string="Generate" class="oe_highlight"/>
Thank you

There is indentation issue in your code, fields and methods which are belongs to class must be the next level in indentation.
class wizard(models.TransientModel):
_name = 'model.name'
department_id = fields.Many2one('hr.department')
employee_id = fields.Many2one('hr.employee')
date_from = fields.Date(string = 'Start Date', required = True)
date_to = fields.Date(string = 'End Date', required = True)
state = fields.Selection([('draft', 'Draft'), ('verify', 'Waiting'), ('done', 'Done'),('cancel', 'Rejected'),])
def generate(self, cr, uid, ids, context=None):
return self.write(cr, uid, ids, {'state': 'draft'}, context=context)

Related

odoo 8 change from models.Model to osv.osv

In my odoo 8 itu use two kind of class, models.Model and osv.osv and I need it to change form models.Model to osv.osv
class purchase_extend2(models.Model):
_inherit = 'purchase.order'
allowed_credit = fields.Float(string="Customer Debt", compute="_get_allowed_credit", )
#api.one
#api.depends('customer_id')
def _get_allowed_credit(self):
invoice_pool = self.env['account.invoice']
confirmed_so_objs = invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', self.customer_id.id)])
self.allowed_credit = sum([so.amount_total for so in confirmed_so_objs])
how to change it into this class?
class purchase_extend2(osv.osv):
Thank you
You need to use a functional field
Example:
from openerp.osv import osv, fields
class PurchaseOrderExtend(osv.osv):
_inherit = 'purchase.order'
def _get_allowed_credit(self, cr, uid, ids, field_name, arg, context):
res = {}
invoice_pool = self.pool.get('account.invoice')
for order in self.browse(cr, uid, ids, context=context):
confirmed_so_objs = invoice_pool.search_read(cr, uid, [('state', '=', 'open'), ('partner_id', '=', order.partner_id.id)], ['amount_total'])
res[order.id] = {'allowed_credit': sum(so['amount_total'] for so in confirmed_so_objs)}
return res
_columns = {
'allowed_credit': fields.function(_get_allowed_credit, type="float", string="Customer Debt", multi='_get_allowed_credit')
}
To use exceptions, use the following import:
from openerp.exceptions import Warning

How to use many2one field in the function in openerp

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'),
}

How to override create methode in odoo 10

i want to use same create method in odoo 10 as below means i want to convert below code in odoo 10, below code is working well for odoo 8
def create(self, cr, uid, vals, context=None):
phase_obj = self.pool.get('hr_evaluation.plan.phase')
survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
if vals.get('user_id'):
user_obj = self.pool.get('res.users')
partner_id = user_obj.read(cr, uid, vals.get('user_id'), fields=['partner_id'], context=context)['partner_id'][0]
else:
partner_id = None
user_input_obj = self.pool.get('survey.user_input')
if not vals.get('deadline'):
vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
'deadline': vals.get('deadline'),
'type': 'link',
'partner_id': partner_id}, context=context)
vals['request_id'] = ret
return super(hr_evaluation_interview, self).create(cr, uid, vals, context=context)
i am trying below code:
def create(self, vals):
survey_id = self.env['hr_evaluation.plan.phase'].read(vals.get('phase_id'),fields=['survey_id'])['survey_id'][0]
if vals.get('user_id'):
partner_id = self.env['res.users'].read(vals.get('user_id'), fields=['partner_id'])['partner_id'][0]
else:
partner_id = None
if not vals.get('deadline'):
vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
ret = self.env['survey.user_input'].create({'survey_id': survey_id,
'deadline': vals.get('deadline'),
'type': 'link',
'partner_id': partner_id})
vals['request_id'] = ret
return super(hr_evaluation_interview, self).create(vals)
but it is giving me error like TypeError: read() got multiple values for keyword argument 'fields' so please guide me how can i remove this error?
read method accept fields as argument and you give it two arguments.
read([fields])
Reads the requested fields for the records in self, low-level/RPC method. In Python code, prefer browse().
Parameters
fields -- list of field names to return (default is all fields)
Returns
a list of dictionaries mapping field names to their values, with one dictionary per record
Raises
AccessError -- if user has no read rights on some of the given records
Instead of calling read method it's better to call browse() method, you can read Browse() vs read() performance in Odoo 8
Your code should be:
def create(self, vals):
survey_id = self.env['hr_evaluation.plan.phase'].browse(vals.get('phase_id'))
if vals.get('user_id'):
partner_id = self.env['res.users'].browse(vals.get('user_id'))
else:
partner_id = None
if not vals.get('deadline'):
vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
ret = self.env['survey.user_input'].create({'survey_id': survey_id.id,
'deadline': vals.get('deadline'),
'type': 'link',
'partner_id': partner_id.id})
vals['request_id'] = ret.id
return super(hr_evaluation_interview, self).create(vals)

How to display name_get() in openerp?

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

take field date from another database

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.