I want to change M2O field domain based on the user selection from selection field:
#api.onchange('search_by')
def _get_partner(self):
partners = self.env['customer.regist'].search([('name','!=','New')])
partner_list = []
partner_list2 = []
for rec in partners:
partner_list.append(rec.name)
partner_list2.append(rec.phone)
res = {}
if self.search_by == 'code':
res['domain'] = {'search_value': [('name', 'in', partner_list)]}
if self.search_by == 'phone':
res['domain'] = {'search_value': [('phone', 'in', partner_list2)]}
return res
but the domain not change and get the default domain from model
Change your function like this.
#api.onchange('search_by')
def _get_partner(self):
partners = self.env['customer.regist'].search([('name','!=','New')])
partner_list = []
partner_list2 = []
for rec in partners:
partner_list.append(rec.name)
partner_list2.append(rec.phone)
res = {}
if self.search_by == 'code':
domain = {'search_value': [('name', 'in', partner_list)]}
return {'domain': domain}
if self.search_by == 'phone':
domain = {'search_value': [('phone', 'in', partner_list2)]}
return {'domain': domain}
Try doing this :
#api.onchange('search_by')
def _get_partner(self):
partners = self.env['customer.regist'].search([('name','!=','New')])
partner_list = []
partner_list2 = []
for rec in partners:
if rec.name :
partner_list.append(rec.name)
if rec.phone:
partner_list2.append(rec.phone)
res = {}
if self.search_by == 'code':
res['domain'] = {'search_value': [('name', 'in', partner_list)]}
if self.search_by == 'phone':
res['domain'] = {'search_value': [('phone', 'in', partner_list2)]}
return res
Make sure that the 'False' values are never added to the domain, since it might give invalid results
Related
enter image description here
This my code and i don't know why this error happen.
#api.model
def update_prices_in_mitra(self):
self.ensure_one()
select_hargamitra = self.env['isd.res.partner.price_list'].search(
[('partner_id', '=', self.mitra_id.id)])
cek = []
# self.task_orderlines = [(5, 0, 0)]
for cek in select_hargamitra.price_list:
if (select_hargamitra and (len(select_hargamitra.price_list) > 0)):
for x in self.task_orderlines:
if cek.product_id.id == self.product_id.id:
x.write({'mitra_price': cek.price_amount,
'isd_mitra_status' : True})
Try this because there might be multiple record in a page i.e. tree view
#api.model
def update_prices_in_mitra(self):
for rec in self:
select_hargamitra = self.env['isd.res.partner.price_list'].search([('partner_id', '=', rec.mitra_id.id)])
cek = []
for cek in select_hargamitra.price_list:
if (select_hargamitra and (len(select_hargamitra.price_list) > 0)):
for x in rec.task_orderlines:
if cek.product_id.id == rec.product_id.id:
x.write({'mitra_price': cek.price_amount,
'isd_mitra_status' : True})
I want to add a group selection button and is_group boolean field to inherited res.partner model but when added the group button the function compute_company_type not working
My code is
class Partner(models.Model):
_inherit = 'res.partner'
company_type = fields.Selection(selection_add=[('group', 'Group')])
refered = fields.Many2one('res.partner',string="Refered By")
import1 = fields.Float(string="Import")
temp_import = fields.Float(string="Temporary Import")
export = fields.Float(string="Export")
temp_export = fields.Float(string="Temporary Export")
transit = fields.Float(string="Transit")
group_id = fields.Many2one('res.group', string='Related Group', index=True)
is_group = fields.Boolean(string='Is a group', default=False)
is_company = fields.Boolean(string='Is a company', default=False)
#api.depends('is_company', 'is_group')
def _compute_company_type(self):
for partner in self:
if partner.is_group:
partner.company_type = 'group'
elif partner.is_company:
partner.company_type = 'company'
else:
partner.company_type = 'person'
def _write_company_type(self):
for partner1 in self:
if partner1.company_type == 'group':
partner1.is_group = partner1.company_type
elif partner1.company_type == 'company':
partner1.is_company = partner1.company_type
else:
partner1.company_type = 'person'
#api.onchange('company_type')
def onchange_company_type(self):
for partner in self:
if partner.company_type == 'group':
partner.is_group = (partner.company_type == 'group')
elif partner.company_type == 'company':
partner.is_company = (partner.company_type == 'company')
else:
partner.company_type = 'person'
I need one more company type button ie group if group button is TRUE need to change fields accordingly in res.partner form
If you want to rewrite _compute_company_type function from res.partner you must inherit your custom model from res.partner, something like this:
from odoo.addons.base.res import res_partner as res_partner
class CRMLeadPropertyMulti(res_partner.Partner):
_inherit = 'res.partner'
## Your Code ##
#api.depends('is_company', 'is_group')
def _compute_company_type(self):
for partner in self:
partner.company_type = 'company' if partner.is_company else 'group' if partner.is_group else 'person'
#api.onchange('company_type')
def onchange_company_type(self):
self.is_company = (self.company_type == 'company')
self.is_group = (self.company_type == 'group')
I think it could be a good idea to make false or true each case evaluating as bool if is a group or a company, that way if one is true the other is false, this can guard the consistency.
Your field is_group is a boolean. I don't know what you meant by this if partner.is_group:.
Boolean field is have only two values and they are true or false. Without checking true or false you won't get any result.
Try with the true or false and let me know.
Thanks and regards in advance.
In my method, I delete lines from stock.pickings and want to add different lines from my model. but i get an error AttributeError: 'stock.move' object has no attribute 'get'
#api.multi
def _action_procurement_create(self):
res = super(SaleOrderLine, self)._action_procurement_create()
order_line_bom = self.env['sale.order.line.bom'].search([('sale_order_line_id', '=', self.id )])
stock_move_lines = self.env['stock.move']
created_stock_move_lines = self.env['stock.move']
vals = {}
for order in self.order_id:
if self.product_id.bom_ids:
order.picking_ids.move_lines.state = 'draft'
for move_line in order.picking_ids.move_lines:
move_line.unlink()
for bom_line in order_line_bom:
vals['product_id'] = bom_line.product_id.id,
vals['product_uom'] = 1,
vals['location_id'] = 1,
vals['name'] = bom_line.product_id.name,
vals['location_dest_id'] = 1,
created_stock_move_lines += stock_move_lines.create(vals)
order.create(stock_move_lines)
You have defined:
stock_move_lines = self.env['stock.move']
Then you try to pass it to create method:
order.create(stock_move_lines)
As documented in model.py
:param dict vals:
values for the model's fields, as a dictionary::
{'field_name': field_value, ...}
see :meth:`~.write` for details
Please try may it's help you :
#api.multi
def _action_procurement_create(self):
res = super(SaleOrderLine, self)._action_procurement_create()
order_line_bom = self.env['sale.order.line.bom'].search([('sale_order_line_id', '=', self.id )])
stock_move_lines = self.env['stock.move']
created_stock_move_lines = self.env['stock.move']
vals = {}
for order in self.order_id:
if self.product_id.bom_ids:
order.picking_ids.move_lines.state = 'draft'
for move_line in order.picking_ids.move_lines:
move_line.unlink()
for bom_line in order_line_bom:
vals = {
'product_id': bom_line.product_id.id,
'product_uom': 1,
'location_id': 1,
'name': bom_line.product_id.name,
'location_dest_id': 1,
}
created_stock_move_lines += stock_move_lines.create(vals)
order.create(stock_move_lines)
I am trying to create a sample order in sales order.In the sample order form, products are sold to customers as complimentary copy(books in my case) without charging any money so I have created a separate sub-menu in sales menu.Here I take only product and quantity as input. Sample order number will be the next number from the sales order(like SO360).So I am fetching sales order number as parent_id in my inherited module.I am not able to create sale order line(data in order lines tab)
class SaleOrder(models.Model):
_inherit = 'sale.order'
# Fields
is_sample = fields.Boolean(string="Sample Order", default=False)
parent_id = fields.Many2one('sale.order', string="Parent Sales Order")
sample_ids = fields.One2many('sale.order', 'parent_id',
string="Sample Orders")
# Methods
#api.model
#api.returns('sale.order')
def create(self, vals):
if vals.get('is_sample', False) and vals.get('name', '/') == '/':
IrSeq = self.env['ir.sequence']
ref = IrSeq.next_by_code('sale.order.sample.ref') or '/'
parent = self.search([('id', '=', vals.get('parent_id'))])
vals['name'] = parent.name + ref
vals['user_id'] = parent.user_id.id
return super(SaleOrder, self).create(vals)
class SampleOrderWizard(models.TransientModel):
_name = 'sale.order.sample.wizard'
_description = 'Sample Sale Order Wizard'
# Field default values
#
def _get_parent(self):
res = False
if self.env.context \
and 'active_id' in list(self.env.context.iterkeys()):
res = self.env.context['active_id']
return res
def _get_new_sale_line(self, orig_sale, orig_sale_line, wizard_line):
"""Internal function to get the fields of the sale order line. Modules
enhancing this one should add their own fields to the return value."""
res = {
'order_id': orig_sale.id,
'product_id': orig_sale_line.product_id.id,
'name': orig_sale_line.name,
'sequence': orig_sale_line.sequence,
'price_unit': orig_sale_line.price_unit,
'product_uom': orig_sale_line.product_uom.id,
'product_uom_qty': wizard_line and wizard_line.qty or 0,
'product_uos_qty': wizard_line and wizard_line.qty or 0,
}
# Simple check for installation of sale_line_code module
if hasattr(orig_sale_line, 'order_line_ref'):
res.update({'order_line_ref': orig_sale_line.order_line_ref})
return res
def _get_order_lines(self, sale):
res = []
for line in sale.order_line:
wizard_line = False
for wzline in self.wizard_lines:
if wzline.product == line.product_id:
wizard_line = wzline
break
if wizard_line:
res.append(
(0, 0, self._get_new_sale_line(sale, line, wizard_line))
)
return res
def _get_wizard_lines(self):
res = []
if self._get_parent():
SaleOrder = self.env['sale.order']
parent = SaleOrder.search([('id', '=', self._get_parent())])
for line in parent.order_line:
res.append((0, 0,
{
'product': line.product_id,
'qty': 1,
}))
return res
# Fields
#
order = fields.Many2one('sale.order', default=_get_parent, readonly=True)
wizard_lines = fields.One2many('sale.order.sample.wizard.line', 'wizard',
default=_get_wizard_lines)
order_date = fields.Datetime(default=fields.Datetime.now())
# Methods
#
#api.one
def create_order(self):
sale_vals = {
'user_id': self.order.user_id.id,
'partner_id': self.order.partner_id.id,
'parent_id': self.order.id,
'date_order': self.order_date,
'client_order_ref': self.order.client_order_ref,
'company_id': self.order.company_id.id,
'is_sample': True,
'order_line': self._get_order_lines(self.order)
}
self.env['sale.order'].create(sale_vals)
return {'type': 'ir.actions.act_window_close'}
class SampleOrderWizardLine(models.TransientModel):
_name = 'sale.order.sample.wizard.line'
_description = 'Sample Order Wizard Line'
wizard = fields.Many2one('sale.order.sample.wizard')
product = fields.Many2one('product.product',
domain=[('sale_ok', '=', True)])
qty = fields.Float(string="Quantity", default=1.0,
digits_compute=dp.get_precision('Product UoS'))
I am getting current login user id by following function
def _get_user_name(self, cr, uid, *args):
user_obj = self.pool.get('res.users')
user_value = user_obj.browse(cr, uid, uid)
return user_value.id or False
and now i want to use its value in this field's Domain like ....
x_trainer_id = fields.Many2one('res.partner', string='Trainer',domain=[('user_id.id','=','get_user_name')])
How is it possible? I'll be very thankful....
you can do it as below:
x_trainer_id = fields.Many2one('res.partner', string='Trainer',domain=lambda self: [('id', '=', self.env.uid)])
pass domain=lambda self: [('id', '=', self.env.uid)]