how to get value of a field in fields_view_get? - odoo

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

Related

Copy last value in new tree view row odoo 9

When click on Add an Item in tree view, I want in new row copy value from last inserted row.
Eg. if field name = 'Text' in new row I need in field name string 'Text'
Any simple solution?
If you want to load default value from a database then follow this method.
You can achieve it by overriding default_get method and in that, you need to write your logic.
#api.model
def default_get(self,fields):
res = super(class_name, self).default_get(fields)
last_rec = self.search([], order='id desc', limit=1)
if last_rec:
res.update({'your_field_name':last_rec.field_value})
return res
While you click on add an item it will fill the new record with its default value and in default value we have written last record's value it it's there.
If you want to load default value from list view (last added value in a list) then it's a bit tricky work, for that you can do something like as follow.
Add one field in the parent form.
last_added_value = fields.Char("Last Added Value")
Create onchange method for that field.
#api.onchange('field_name')
def onchange_fieldname(self):
# there must be many2one field of parent model, use it here.
self.parent_model_field.last_added_value = self.field_name
And in xml field, you need to write like this.
<field name="one2many_field" context="{'default_field_name' : parent.last_added_value}">
<tree string="Title" editable="bottom">
<field name="field_name"/>
</tree>
</field>
You also need to write default_get method.
#api.model
def default_get(self,fields):
res = super(class_name, self).default_get(fields)
last_rec = self.search([('parent_field_id','=',self.parent_field_id.id)], order='id desc', limit=1)
if last_rec:
res.update({'your_field_name':last_rec.field_value})
return res

How to assign a value of selection field to other selection field in a onchange method in odoo?

Just working on the following code to autofill a Selection field
calendar.event has a location field which is a selection field, trying to autofill it in my custom module based upon an onchange method.
I wanted to get the selected value in that selection field for a particular record into 'loc' field which is also a selection field in my custom module
def get_meet_dets(self, cr, uid, ids, meet_ref, context=None):
val = {}
res = []
if meet_ref:
for det in self.pool.get('calendar.event').browse(cr,uid,meet_ref,context=context):
for asst in det.attendee_ids:
emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id','in',user_id)])
val = {
'empname' : emp_id[0],
'wk_mail': asst.partner_id.email,
'loc' : det.location,
}
res.append(val)
val.update({'matp':res})
and 'loc' is a selection field in current class. Anyone having any idea on this?
You need to pass an existing id for your loc field, you can try 'loc' : det.location.id,. I hope this can be helpful for you.

Openerp get partner category tags

I have 2 fields in my custom module:
'originator_id' : fields.many2one("res.partner",string="Originator", required=True),
'originator_category_ids' : fields.many2many('res.partner.category',
'module_category_rel',
'module_id',
'category_id',
'Categories'),
I want to set the domain for the many2many field "originator_category_ids" according to the selected "originator_id" which is a partner_id. I wrote an onchange method to define the domain dynamically:
def get_domain_originator_category_ids(self,cr,uid,ids,originator_id,context=None):
if originator_id:
obj = self.pool.get('res.partner').browse(cr, uid, originator_id)
return {'domain':{'originator_category_ids':[('id','in',obj.category_id)]}}
But above doesn't work.
Your support will be much appreciated.
This is worked for me, but it is a temporary solution until I find a better one. The solution consist on looping on categories and compare with the selected partner in the partner_ids field:
def get_domain_originator_category_ids(self,cr,uid,ids,originator_id,context=None):
category_obj = self.pool.get('res.partner.category')
category_ids = category_obj.search(cr, uid,[], context=context)
res=[]
for cateory in category_obj.browse(cr, uid, category_ids, context=context):
for partner_id in cateory.partner_ids:
if partner_id.id == originator_id:
res.append(cateory.id)
return {'domain':{'originator_category_ids':[('id','in',res)]}}
If you get a better solution please post it.

How to use field value of one object in another using self.pool.get in openerp?

This is my code in .py file.I want to fetch value of field list_price in product.product and use it in my custom module which inherits sale.order.
Can i store the value of list_price field in my custom field i.e qty_available?
When i print value of wg_qty_avail it shows None even list_price is having value 2000
class practice(osv.osv):
_inherit = 'sale.order'
_columns = {
'qty_available': fields.float('Quantity'),
}
def get_val(self, cr, uid, id, product, context=None):
result={}
wg_qty_avail = self.pool.get('product.product').browse(cr, uid,product,context=context).list_price
print "---------------------------", wg_qty_avail
result['qty_available'] = wg_qty_avail
practice()
xml file is ok..it calls the method get_val by a button click.
Please help.Where am i wrong..
You are not assigning the value to 'qty_available' field correctly
Remove result['qty_available'] = wg_qty_avail
return {'value': {'qty_available':wg_qty_avail}}
Hope this helps...

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.