Get the value of one selection field - odoo

How I affect the value of a field section to a field char.
return { value{'field_char' : field_selection.value !!!}}

If you need to affect it as in onchange event, you just set a parameter to your function with the value you whant of the selection:
<field name="field_selection" onchange="do_change(field_selection)" />
and then, in your function get the parameter, and affect it to your field_char:
def do_change(self, cr, uid, ids, selection_val, context=None):
return {
value{
'field_char' : selection_val
}
}

related to
field1: fields.selection([('a','A')], 'Field1'),
field2: fields.char('Field2'),
just try to
fields2 = fields['a']
only pass key it's return value of key

Override create and write method for that,
def create(self, cr, uid, vals, context={}):
if not vals.get('field_selection',False):
vals.update('field_char') = vals.get('field_selection','')
return super(class_name,self).create(cr, uid, vals, context=context)
def write(self, cr, uid, ids, vals, context={}):
if not vals.get('field_selection',False):
vals.update('field_char') = vals.get('field_selection','')
return super(class_name,self).write(cr, uid, ids, vals, context=context)

Related

odoo 8 remove sum of total in group by

I'm using odoo 8 and I want to remove the sum of the total in the group by. Here is my .py
class report_sales_weekly(osv.osv):
_name = "report.sales.weekly"
_description = "report sales weekly"
_columns = {
'div_target_monthly':fields.float('Div Target'),
'div_achievement':fields.float('Div Achievement'),
}
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
if 'div_target_monthly' in fields:
fields.remove('div_target_monthly')
return super(report_sales_weekly, self).read_group(cr, uid, domain, fields, groupby, offset, limit=limit, context=context, orderby=orderby)
report_sales_weekly()
I found this script from https://www.odoo.com/forum/help-1/how-to-remove-sum-of-total-in-group-by-29666, but I get an error when I make grouping in the list page
TypeError: read_group() got an unexpected keyword argument 'lazy'
Any help please? Thank you
The answer in the link is for openerp-7
Odoo 8 defines an optional parameter lazy, you can find an example in account module on how to override the read_group function:
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
# Your code
return super(account_entries_report, self).read_group(cr, uid, domain, fields, groupby, offset, limit, context, orderby, lazy)
You have two errors here: offset called without offset= and bad definition of the base read_group of Odoo 8.
Can you please try with this :
class report_sales_weekly(osv.osv):
_name = "report.sales.weekly"
_description = "report sales weekly"
_columns = {
'div_target_monthly':fields.float('Div Target'),
'div_achievement':fields.float('Div Achievement'),
}
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=False):
if 'div_target_monthly' in fields:
fields.remove('div_target_monthly')
return super(report_sales_weekly, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby, lazy=lazy)
# As stated in the comments, you can also not put offset=offset etc. but just the variables offset, limit, ... Thanks #Kenly
# Also, why this line ?
# report_sales_weekly()
Keep me updated :)

cannot access properties openERP

I'm new to OpenERP, I have problem accessing properties (fields) of object/record:
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context={}, toolbar=False):
result = super(extended_shipments_wz, self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar)
shipment_id = self._get_active_id(cr, uid, view_id, context)
shipment_obj = self.pool.get('stock.picking.in').browse(cr, uid, shipment_id)
#some more code
#shipment_obj.origin, etc... fails
return result
shipment_id gives me correct ID. shipment_obj is returned (not null) when I try to access any shipment_obj properties (just for testing) it gives me error:
LINE 1: ...ng.id FROM "stock_picking" WHERE stock_picking.id IN (false)... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Copy one2many field SNAFU

I need to show record from notebook project on notebook sheet , but the showed record is not according to project
My py :
class notebook_project(osv.osv):
_name = "notebook.project"
_description = "Notebook Project ID"
def onchange_project(self, cr, uid, ids, project, arg, context=None):
if project :
proj = self.pool.get('project.project').browse(cr, uid, project, context=context)
return {'value': {'name': proj.name}}
return {}
_columns = {
'name' : fields.char('Name', size=64),
'project' : fields.many2one('project.project', 'Project'),
'notebook_project_lines' : fields.one2many('notebook.project', 'notebook_project_id', 'Members Lines'),
'notebook_project_id': fields.many2one('notebook.project', 'Parent Project', ondelete='cascade', select=True),
'member' : fields.many2one('hr.employee', 'Members'),
}
notebook_project()
class notebook_sheet(osv.osv):
_name = "notebook.sheet"
_description = "Notebook Project Sheet"
def onchange_notebook_project(self, cr, uid, ids, notebook_project, context=None):
res = {}
employee_lines = []
if not notebook_project : return {}
if notebook_project :
notebook_project_obj = self.pool.get('notebook.project').browse(cr, uid, notebook_project)
for p in notebook_project_obj.notebook_project_lines:
employee_lines.append((0,0,{'notebook_sheet_lines':p.id
}))#this dict contain keys which are fields of one2many field
res['notebook_sheet_lines']=employee_lines
return res
def onchange_project(self, cr, uid, ids, project, context=None):
if project :
proj = self.pool.get('project.project').browse(cr, uid, project, context=context)
return {'value': {'name': proj.name}}
return {}
#def create(self, cr, user, vals, context={}):
#first model
# notebook_project_obj = self.pool.get('notebook.project')
#browse and get o2m fields, according to your selected project(id)
# notebook_project_lines = notebook_project_obj.browse(cr, user, ['notebook_project_id'])[0].lines
#copy first o2m model to second o2m model
# for line in notebook_project_lines :
# vals['notebook_sheet_lines'].append([0, False, {'notebook_project_lines':line.employee_id.id,}])
# return super(notebook_sheet, self).create(cr, user, vals, context)
_columns = {
'name' : fields.char('Name', size=64),
'notebook_sheet_lines' : fields.many2many('notebook.project', 'notebook_project_sheet_rel', 'notebook_project', 'notebook_project_id'),
'notebook_project': fields.many2one('notebook.project', 'Project ID',domain=[('notebook_project_id','=',False)]),
'project' : fields.many2one('project.project', 'Project'),
'member' : fields.many2one('hr.employee', 'Members'),
}
notebook_sheet()
Edited my answer , Mr. AnomA . Still not sure about many2many , please kindly check it
Do i need to change the onhange event as well ? thanks again in advance
First of all please create a link between notebook.sheet and notebook.project. Now there is no link between these 2 models. Add a many2one relation to notebook.sheet in notebook.project and then change the relation id in notebook_sheets_lines to this field.
Otherwise change the relation type of notebook_sheet_lines to many2many.
Then change the onchange_notebok_project() which will return {'value':{'notebook_sheet_lines':LIST_OF_EMPOYEEIDS}}
Also No need to add many2many in motebook_project.

Messaging System of Openerp v7

Can anyone share how I can change the "from" field value when sending a message because it always has the same email address?
--the address of the outgoing mail server I configure.
You can even send mail without making use of email template.
You can use mail.message & mail.mail objects.
def send_mail(cr, uid, ids, context=context):
mail_server_obj = self.pool.get('ir.mail_server')
mail_message_obj = self.pool.get('mail.message')
mail_mail_obj = self.pool.get('mail.mail')
for id in ids:
mail_message_id = mail_message_obj.create(cr, uid, {'email_from': 'from_add', 'model': 'model_name', 'res_id': id, 'subject': 'subject_name', 'body': 'your_html_body'}, context=context)
mail_server_ids = mail_server_obj.search(cr, uid, [], context=context)
mail_mail_id = mail_mail_obj.create(cr, uid, {'mail_message_id': mail_message_id, 'mail_server_id': mail_server_ids and mail_server_ids[0], 'state': 'outgoing', 'email_from': 'from_add', 'email_to': 'to_add', 'body_html': 'your_html_body'}, context=context)
if mail_mail_id:
mail_mail_obj.send(cr, uid, [mail_mail_id], context=context)
return True
There are many ways of sending mails. The good way is by creating a email template.
First create one email template.
def send_email(self, cr, uid, ids, context=None):
email_template_obj = self.pool.get('email.template')
template_ids = email_template_obj.search(cr, uid, [('model_id.model', '=', 'sale.order')])
if template_ids:
for id in ids:
values = email_template_obj.generate_email(cr, uid, template_ids[0], id, context=context)
print "values:: ", values
values['subject'] = your_subject
values['email_to'] = your_mail_to_address
values['email_cc'] = your_cc_address
values['body_html'] = your_body_html_part
values['body'] = your_body_html_part
mail_mail_obj = self.pool.get('mail.mail')
msg_id = mail_mail_obj.create(cr, uid, values, context=context)
if msg_id:
mail_mail_obj.send(cr, uid, [msg_id], context=context)
return True
Hope this will solve your problem.
Thank you.
Change your email preference from the top right menu showing your login.

How can i use onchange function to change price of account invoice line price_unit

I would like to use onchange to set account invoice price to a default price set in res.partner. Could anyone give me an example of this ?
Thanks,
Joe
There are lots of examples of onchange in addons.
For example, if you want to change the value of 'name' field and want to set product's name based on selected product:
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
return {'value': {'name': prod.name}}
return {}
Write this in your_invoice_view.xml:
<field name="product_id" on_change="product_id_change(product_id, parent.partner_id, context)"/>
Write onchange in your_invoice.py under "invoice_line" model(class):
def onchange_product_id_change(self, cr, uid, ids, product_id, partner_id context=None):
if product_id and partner_id:
product_brw = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
partner_brw = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
/*write your logic */
return {'value': {'price_unit': your calculated value}}
return {}