Openerp get partner category tags - odoo

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.

Related

odoo 8 Count attachment and show in list view

I have odoo 8. I want to count the attachment from ir_attachment and and show it in stock.production.lot. Here is my .py
class stock_production_lot(models.Model):
_inherit='stock.production.lot'
#api.multi
def get_attachment_info(self):
for lot in self:
so_line_ids = self.env['ir.attachment'].search([('res_id','=',lot.id)])
for pick in so_line_ids:
pick.count_attachment = 0
if pick.datas_fname:
pick.count_attachment = len(pick.datas_fname)
count_attachment = fields.Float(string='Total Attachment', compute='get_attachment_info')
and this the view
<field name="count_attachment" />
Thanks
It's difficult to answer, because the information in your question are a bit poor. But let me try to answer it in a general way with a general example how i would do it.
Let's say you want a count of all attachments of model stock.picking (Delivery Nots, Slips, Receipts, and so on).
So you need to add a computed field, which could be stored, but it's difficult to trigger a recalculation of this field, because attachments are related to records indirectly (no real foreign keys used in database).
class StockPicking(models.Model):
_inherit = 'stock.picking'
attachment_count = fields.Integer(compute="_compute_attachment_count")
#api.multi
def _compute_attachment_count(self):
for picking in self:
domain = [('res_id', '=', picking.id),
('res_model', '=', self._name)]
picking.attachment_count = self.search_count(domain)
And also add the new field to a view of model stock.picking.
Now let's pretend that you don't only want the attachments of the pickings but also of their move lines, too.
Just "count" them and add that count to the previous one:
#api.multi
def _compute_attachment_count(self):
for picking in self:
domain = [('res_id', '=', picking.id),
('res_model', '=', self._name)]
picking_count = self.search_count(domain)
if picking.move_lines:
domain_move = [('res_id', 'in', picking.move_lines.ids),
('res_model', '=', picking.move_lines._name)]
picking_count += picking.move_lines.search_count(domain_move)
picking.attachment_count = picking_count
Thank you for the respone. I have got the answer
#api.one
def count_attachments(self):
obj_attachment = self.env['ir.attachment']
for record in self:
record.count_attachment = 0
attachment_ids = obj_attachment.search([('res_model','=',self._name),('res_id','=',record.id)])
if attachment_ids:
record.count_attachment = len(attachment_ids)

separate attribute from name products (Odoo 14)

sorry my english. I need a help with a issue I have. I defined a funtion to bring values attributes products however, when I run it, the result is
ValueError: Expected singleton: product.template.attribute.value(9, 25)
will somebody guide me to solve it? I dont know how to go on
class MRPSalesProduc(models.Model):
_inherit = 'mrp.production'
product_short_name = fields.Char('Productos')
#api.model
def create(self, vals):
product_short_name = self.env['sale.order'].search([('name', '=', vals[
'origin'])]).order_line.product_id.product_template_attribute_value.attribute_id
vals['product_short_name'] = product_short_nam
rec = super(MRPSalesProduc, self).create(vals)
return rec
You can use a related many2many field to get product attributes
Example:
class MRPSalesProduct(models.Model):
_inherit = 'mrp.production'
product_template_attribute_value_ids = fields.Many2many(related='product_id.product_template_attribute_value_ids')
Then use the man2many_tags widget to show the product attributes as tags:
<field name="product_template_attribute_value_ids" widget="many2many_tags"/>
Example (Product name):
class MRPProductsName(models.Model):
_inherit = 'mrp.production'
products_name = fields.Char(related="product_id.product_tmpl_id.name", string='Productos')
You have tried to search sale order and access all sale order lines.
If you pick first line from the sale order line, then it will be work as per your expectation. For example,
sale_order = self.env['sale.order'].search([('name', '=', vals['origin'])])
product_short_name = sale_order and sale_order.order_line[0].product_id.product_template_attribute_value.attribute_id
if product_short_name:
vals['product_short_name'] = product_short_nam
You can reference my blog https://odedrabhavesh.blogspot.com/2017/02/valueerror-expected-singleton-in-odoo.html for more about "ValueError: Expected singleton"

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

One module field use to other module in openerp

I created a field name "link to opportunities" :-
module :- hr.applicant
field type:- many2many
object relation:- crm.lead
and i used in crm.lead module .
Now i want to use this field in "hr.recruitment" .
but i have tried many ways but not success. please tell me. how can use this field in other module like as crm.lead to hr.recruitment
thank you for your timing.
this code i used:-
'sale_o_ids' : fields.related('job_id', 'x_link_to_jobposition',
readonly=True,
relation='crm.lead',
string='Opportunity Name'),
Here is the example:
of many2many
class hr_job(osv.osv):
_inherit = 'hr.job'
_columns = {
'sale_ids': fields.many2many('sale.order', 'hr_job_sale_order_rel', 'job_id', 'sale_id', 'Sale order'),
}
hr_job()
Here created a many2many field of sale.order
Now i want to used the hr.job field in hr.employee.
class hr_employee(osv.osv):
_inherit = "hr.employee"
def _get_sale_order(self, cr, uid, ids, field_name, arg, context=None):
if context is None:
context = {}
result = {}
list_o = []
for order in self.browse(cr, uid, ids, context=context):
for i in order.job_id.sale_ids:
list_o.append(i.id)
result[order.id] = list_o
return result
_columns = {
'sale_order_ids': fields.function(_get_sale_order, type='many2many', relation="sale.order", string="Sale Orders"),
}
hr_employee()
So when you update in the hr.job many2many field then its updated value show in hr.employee object when in job select this job
Another method you can use related
'sale_o_ids' : fields.related('job_id', 'sale_ids',
type='many2many',
readonly=True,
relation='sale.order',
string='Available Sale Order'),
Hope this thing clear to you

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.