cannot access properties openERP - odoo

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.

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 :)

Get the value of one selection field

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)

how to get value of a field in fields_view_get?

I want to get a value of a field in fields_view_get method in openerp 7.0.
I tried the following:
1- send the value of the field in the context attribute as following:
< field name="employee_id" context="{'employee_id':employee_id}" />
and in the fields_view_get I get it as following:
print "employee_id in the context value is %s"%(context.get('employee_id', False))
but it always the the context.get(...) returns False. so I tried the following:
2- on the onchange method of the field I send the value of the field in the context as following:
def onchange_employee_id(self, cr, uid, ids, employee_id):
return {'context': {'employee_id': employee_id}}
and in the fields_view_get I get it as following:
print "employee_id in the context value is %s"%(context.get('employee_id', False))
but also the same thing always the context.get(..) returns False.
How can I get the value of a field in fields_view_get function ?
Maybe this answer is too late for you, but perhaps someone will find it useful.
If you need the dynamic view just on form view, you should write a tree view and you can put the selected record id to the context...so with the context id, you can read the fields.
But fields_view_get is not too easy. Dont forget about update the return dictionary (the two very important keys: fields, arch).
If you want to use invisible or readonly tag, you should use modifiers tag like attrs.
Example:
def fields_view_get(self, cr, uid, view_id=False, view_type='tree', context=None, toolbar=False, submenu=False):
fields = self.read(cr, uid, context['working_id'], [])
actualView = super(ModelName, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
# you can write default view in xml and dynamic complete with some field in this method
actualView['fields'].update({'field_name':{'type': 'text', 'string': 'Name'}})
arch = minidom.parseString(actualView['arch'])
#for example: triggered to <newline/> field
newlineField = arch.getElementByTagName('newline').item(0)
element = arch.createElement('field_name')
element.setAttribute('name', 'Name')
newlineField.insertBefore(element, 0)
actualView['arch'] = arch.toxml("utf-8")
return actualView

Openerp - AttributeError in function of stock.picking.in

Really struggling with this one:
I have inherited from stock.picking.in and have added a few columns. I then added a function field.
In the function that the function field refers to, it works if I do not use any attribute from the stock.picking.in object. The moment I use any value from the object, it starts giving 'AttributeError: ' and some attribute at random. It doesn't specify any other reasons or causes.
Code:
class stock_picking_custom(osv.osv):
_name = 'stock.picking.in'
_inherit = 'stock.picking.in'
_table = "stock_picking"
def calc_royalty(self, cr, uid, ids, field_name, arg, context=None):
if not ids: return {}
res = {}
for line in self.browse(cr, uid, ids, context=context):
res[line.id] = 0 #line.royalty_rate * line.loading_netweight
return res
_columns = {
'loading_netweight': fields.float('Net weight at loading', digits=(16,2), help="Net weight at loading (interior weight)"),
'royalty_date': fields.date('Royalty Issue date'),
'royalty_number' : fields.char('Royalty Number', size=64),
'royalty_rate' : fields.float('Royalty Rate (p. ton)', digits=(16,2)),
'royalty_amount' : fields.function(calc_royalty, type='float', digits=(16,2), string='Royalty Amount', store=True, select=True)
}
stock_picking_custom()
I have commented out the line that I want to use. The moment I put this line back in the code, it would give attribute error on royalty_date (for example) which is not even mentioned in the function.
Please guide.
EDIT: I tried the exact same code with purchase.order and it works perfectly. What is different about stock.picking.in?
Thanks
Ok, found the answer in stock module in delivery addon. So this is a framework limitation issue related to inheritance order etc.
Sharing here in case someone ends up in a similar situation.
To solve, I repeated the same fields in stock.picking and stock.picking.in. Then I called the calc function of the picking class from the picking.in class.
Code:
class stock_picking_custom(osv.osv):
_name = 'stock.picking'
_inherit = 'stock.picking'
def calc_royalty(self, cr, uid, ids, field_name, arg, context=None):
if not ids: return {}
res = {}
for line in self.browse(cr, uid, ids, context=context):
res[line.id] = line.royalty_rate * line.loading_netweight
return res
_columns = {
'loading_netweight': fields.float('Net weight at loading', digits=(16,2), help="Net weight at loading (interior weight)"),
'royalty_date': fields.date('Royalty Issue date'),
'royalty_number' : fields.char('Royalty Number', size=64),
'royalty_rate' : fields.float('Royalty Rate (p. ton)', digits=(16,2)),
'royalty_amount' : fields.function(calc_royalty, type='float', digits=(16,2), string='Royalty Amount', store=True, select=True)
}
stock_picking_custom()
class stock_picking_in_custom(osv.osv):
_name = 'stock.picking.in'
_inherit = 'stock.picking.in'
_table = "stock_picking"
def calc_royalty(self, cr, uid, ids, field_name, arg, context=None):
return self.pool.get('stock.picking').calc_royalty(cr,uid,ids,field_name,arg,context=context)
_columns = {
'loading_netweight': fields.float('Net weight at loading', digits=(16,2), help="Net weight at loading (interior weight)"),
'royalty_date': fields.date('Royalty Issue date'),
'royalty_number' : fields.char('Royalty Number', size=64),
'royalty_rate' : fields.float('Royalty Rate (p. ton)', digits=(16,2)),
'royalty_amount' : fields.function(calc_royalty, type='float', digits=(16,2), string='Royalty Amount', store=True, select=True)
}
stock_picking_in_custom()
I did not get much time to spend on it but I came to know that line is coming with stock.picking.in object and fields, you defined, are stored in stock_picking table that's why it may going to search that field with stock.picking.in, not getting and error is coming.
There may be issue with fields defined in object and table, but not sure.

How to make domain dynamic in OpenERP

I want to give a dynamic domain equation in view or in field definition in .py.
like
<field name="product_id" domain="[('name','in',get_names)]"/>
product_id is a many2one field.
get_names is function that creates a list at run time.
Its showing an error - "name 'get_names' is not defined"
Any Ideas.
I have also tried the following.
'product_id': fields.many2one('mymodule.relation.model','Title',selection=get_names)
This displays all entries in mymodule.relation.model. The only thing it does is to validate if value selected/submittted by user belongs to the 'get_names'.
inherit the fields_view_get() function and manage the domain condition. Please check these posts
How to create a dynamic view on OpenERP
How can I change the choices in an OpenERP selection field based on other field values?
1- You can use function field like this:
def _get_domain(self, cr, uid, ids, field_name, arg, context=None):
record_id = ids[0]
# do some computations....
return {record_id: YOUR DOMAIN}
and function field:
'domain_field': fields.function(_get_domain, type='char', size=255, method=True, string="Domain"),
And use the name of the field in xml (domain attr):
<field name="product_id" domain="domain_field" />
2- you can use 'fields_view_get':
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
res = super(taskmng_task, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
doc = etree.XML(res['arch'])
for node in doc.xpath("//field[#name='project_id']"):
# do some computations....
node.set('domain', YOUR DOMAIN)
res['arch'] = etree.tostring(doc)
return res
You can't use a function or method in the domain expression, only object fields.
It's not equivalent, but closest thing is to create a function field to use in the domain expression.
As Don't know your exact requirement .but may be any one from these 2 can help you
http://ruchir-shukla.blogspot.in/2010/11/domains-value-depending-on-condition.html
or check the Account Invoice product onchange . You can return domain from the onchange.