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"
Related
I want to get id from url when i click the button.
This is URL, id is 69:
http://localhost:8069/web#id=69&cids=1&menu_id=385&action=507&model=inventory.menu&view_type=form
I need to get this id in many2one field.
This is my wizard py file:
from odoo import api,fields,models
class ReduceInventoryWizard(models.TransientModel):
_name = "reduce.inventory.wizard"
_description = "Reduce Inventory Wizard"
inventory_ids = fields.Many2one('inventory.menu', string="Ürün Referans No: ", default=lambda self: self.env['inventory.menu'].search([('id', '=', 69)], limit=1))
As you can see, ('id', '=', 69) this is running but just one product. I want the information of that product to come automatically when I click the button in which product.
I tried this one: ('id', '=', self.id). But is not working.
In this situation there should be active_id or better active_ids in Odoo's context.
So you just can set the default parameter to use a default method, which will either return a value or an empty recordset:
def default_my_many2one_field_id(self):
active_ids = self.env.context.get("active_ids")
if active_ids:
return self.env["another.model"].browse(active_ids[0])
return self.env["another.model"]
my_many2one_field_id = fields.Many2one(
comodel_name="another.model", string="My M2o Field",
default=default_my_many2one_field_id)
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)
Put fields of an employee in accounting through a module
Hi. I am creating a module for a client that has a specific need. He wants us to add a price per hour to employees. That is, the price charged by the employee each hour of work. Then create a small expense report that goes into accounting.
The first thing I have done is to modify the employee module using _inherit to add two fields. Nickname that allows employees to filter by nickname. And the hourly price of that employee (what this employee charges for every hour).
employee example changes
The second has been to create a new model that allows adding employees and importing said data. In addition to adding a description.
form example
The challenge now is to link this information to the accounting module so that it will appreciate as a Journal Item and then have the copy confirmed to be appreciated as a Journal Entries.
I am really new in the development of odoo and there are many things that I am still assimilating. So the questions I have are the following:
How could I do this?
Do I have a problem with what I have done so far?
It is my first post and I would appreciate the help. Thanks in advance.
This its the code:
class EmpleadoObra(models.Model):
_inherit = 'hr.employee'
apodo = fields.Char('apodo', readonly=False, store=True)
precio_por_hora = fields.Float('Salario por hora', store=True)
#api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
args = args or []
recs = self.browse()
if name:
recs = self.search(['|', ('apodo', 'ilike', name), ('name', operator, name) ] + args, limit=limit)
return recs.name_get()
class EmpleadosProductos(models.Model):
_name = "employee.as.product"
# _inherits = {'hr.employee' : 'empleado_id'}
employee_line = fields.One2many(
'employee.line',
'id',
string='Employee Lines'
)
class EmployeLine(models.Model):
_name = 'employee.line'
descripcion = fields.Text(string='Descripción', required=False)
employee_id = fields.Many2one(
'hr.employee',
string="Empleado",
requiered=True,
change_default=True
)
apodo = fields.Char('apodo', readonly=False)
precio_por_hora = fields.Float('precio_por_hora')
_rec_name = 'apodo'
#api.onchange('employee_id')
def onchange_employee_id(self):
addr = {}
if not self.employee_id.display_name:
return addr
if not self.employee_id.apodo:
self.apodo = "no apodo"
else:
self.apodo = self.employee_id.apodo
self.precio_por_hora = self.employee_id.precio_por_hora
return addr
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.
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