About the onchange method returning domain or warning in odoo8 - onchange

I tried following the odoo8 official example,but not works for me:
class TestMove(models.Model):
_name = 'test.move'
_description = 'Stock Requisition Return'
num1 = fields.Float(string='num1')
num_float = fields.Float(string='num_float', compute="_compute_num_float", store=True)
num2 = fields.Integer(string='num2')
age = fields.Integer(compute='_compute_age')
age2 =fields.Integer(compute='_compute_age2', store=True)
now_list_price = fields.Float(
string='Now List Price', help='Now List Price', digits=(10, 10))
product_id = fields.Many2one(comodel_name='product.product')
......
#api.one
#api.onchange('num1')
def _onchange_num2(self):
now_ = self.num1 + 1
_logger.info('num2 is:%s', now_)
self.update({'num2': now_})
return {
'domain': {'product_id': [('id', 'in', [now_])]},
'warning': {'title': "Warning", 'message': "What is this?"}
}
class ProductProduct(models.Model):
_name = 'product.product'
_description = 'Product'
_rec_name = 'name'
name = fields.Char()
price_unit = fields.Float(default=Decimal('13.14'))
num2 has changed, but domain and warning are not returned;
Any suggestion please. Thanks in advance.

The issue is caused by #api.one decorator, just remove it and the onchange method should work correctly.

Related

Update records on one2many fields in wizard for odoo16

Geting Issue 'TypeError: unhashable type: 'dict' for insert values in one2many field from onchange method in odoo16
My code is below:
class EmployeeAddWizard(models.TransientModel):
_name = 'employee.add.wizard'
line_ids = fields.One2many('employee.goal.add.line', 'wizard_id', string="Lines")
#api.onchange('challenge_id', 'employee_id')
def _onchange_action_goal_add(self):
r = []
value = {}
self.line_ids = {}
if self.challenge_id and self.employee_id:
goal_records = self.env['gamification.challenge.line'].search([('challenge_id', '=', self.challenge_id.id)])
for emp in self.employee_id:
for line in goal_records:
data = {'wizard_id': self.id, # Other table m2o
'goal_definition_id': line.definition_id.id,
'goal_rating': 0.0,
'goal_target': line.target_goal,
'employee_id': emp.id,
}
r.append(data)
value.update(records=r)
self.line_ids = value['records']
class GoalLine(models.Model):
_name = 'employee.goal.add.line'
wizard_id = fields.Integer()
goal_definition_id = fields.Many2one('gamification.goal.definition', string='Goal Definition', required=True, ondelete="cascade")
goal_rating = fields.Float('Rating', required=True)
goal_target = fields.Float('Target Value ', required=True)
employee_id = fields.Many2one('res.users', string="Employee", required=True, ondelete="cascade")
Thanks in advance
You passed a list of dicts which is not valid, you need to use special commands
Example:
r.append(Command.create(data))
or:
r.append((0, 0, data))
You can use Command.clear(), to remove previous lines if needed ( self.line_ids = {} should raise an error: ValueError: Wrong value).
Check this answer

referenced in foreign key constraint does not exist in Many2many relation in odoo 15

Here is Model definition:
courier.cod.settings
from odoo import fields,models
class CodSetting(models.Model):
_name = 'courier.cod.settings'
_description = 'Code Settings'
merchant_id = fields.Many2one('res.partner', 'Merchant')
pickup_point_groups_ids = fields.Many2many('courier.pickup.point.groups', string='Pickup Point')
delivery_point_groups_ids = fields.Many2many('courier.delivery.point.groups', string='Delivery Point')
cod_charge = fields.Float('Cod Charge')
minimum_cod_charge = fields.Float('Minimum Cod Charge')
enabled = fields.Boolean(string="Active", default=True)
courier.pickup.point.groups
from odoo import fields,models
class PickupPointGroup(models.Model):
_name = 'courier.pickup.point.groups'
_description = 'Pickup Point Grouping'
title = fields.Char(string='Group Name', required=True, translate=True)
upazilla_ids = fields.Many2many('courier.upazillas','upazilla_id', string='Upazilla')
pickup_point_groups_id = fields.Many2one('courier.cod.settings', 'Cod Setting Line')
courier.delivery.point.groups
from odoo import fields,models
class DeliveryPointGroup(models.Model):
_name = 'courier.delivery.point.groups'
_description = 'Delivery Point Grouping'
title = fields.Char(string='Group Name', required=True, translate=True)
upazilla_ids = fields.Many2many('courier.upazillas','upazilla_id', string='Upazilla')
delivery_point_groups_id = fields.Many2one('courier.cod.settings', 'Cod Setting Line')
When I remove courier.delivery.point.groups everything works fine but when I add this as like courier.pickuup.point.groups I am getting https://prnt.sc/5sAnsTVCv6Ck this error. I am using odoo 15. Any idea?

Odoo - Filter Many2one not by _rec_name

I would like to filter m2o field, but not by default name (_rec_name).
class LecturerWorkday(models.Model):
_name = 'lecturer.workday'
_rec_name = 'lecturer_id'
name = fields.Selection([('sunday','Sunday'),('monday','Monday'),('tuesday','Tuesday'),
('wednesday','Wednesday'),('thursday','Thursday'),('friday','Friday'),('saturday','Saturday'),
], default='sunday',string="Workday", required=True)
lecturer_id = fields.Many2one('school.lecturer', string="Lecturer Name", invisible=True)
class SchoolLecturer(models.Model):
_name = 'school.lecturer'
name = fields.Char(string="Lecturer Name", required=True)
workday_id = fields.Many2one("lecturer.workday", string="Workday ID")
class LecturerTimeoff(models.Model):
_name = "lecturer.timeoff"
lecturer = fields.Many2one('school.lecturer', string="Lecturer Name")
day_m2o = fields.Many2one('lecturer.workday', string="Lecturer Workdays")
reason = fields.Char("Time off Reason")
#api.onchange('lecturer')
def get_lecturer_workday(self):
day_obj = self.env['lecturer.workday'].search([('lecturer_id', '=', self.lecturer.id)]).mapped('name')
day_list = []
for rec in day_obj:
day_list.append(rec)
res = {}
res['domain'] = {'day_m2o': [('name', '=', day_list)]}
return res
print (res)
My question are:
When I choose lecturer name, day_m2o should display the workday of selected lecturer name. I have been trying to compute it as above, but the result is still display lecturer name, instead of workday.
It seems that #api.onchange didn't update the result instantly whenever i clicked the new lecturer name who didn't have workday yet. How to fix this?
Thanks for your help

How to track One2many field in Odoo12?

I am trying to log the changes on a One2many field using track_visibility='onchange'. But it's not working.
Here is the code:
respartner.py
bank_account_ids = fields.One2many('customer.bank.account','partner_id',
string='Account',track_visibility="onchange")
account.py
_name = 'customer.bank.account'
_description = 'Partner Bank Account Details'
partner_id = fields.Many2one('res.partner',string="Partner")
name = fields.Integer(string="Account Number",required=True,
track_visibility="onchange")
bank_id = fields.Many2one('partner.bank',string="Bank",track_visibility="onchange")
branch_id = fields.Many2one('partner.bank.branch',string="Branch",
track_visibility="onchange")
Yes, No need to touch ORM. Try this
class ParentClass(models.Model):
_name = 'parent.class'
_inherit = ['mail.thread']
child_ids = fields.One2many('child.class', 'relational_field_name_id')
class ChildClass(models.Model):
_name = 'child.class'
_inherit = ['mail.thread']
name = fields.Char(tracking=True) # Note that tracking is true here
relational_field_name_id = fields.Many2one('parent.class')
def write(self, vals):
super().write(vals)
if set(vals) & set(self._get_tracked_fields()):
self._track_changes(self.relational_field_name_id)
def _track_changes(self, field_to_track):
if self.message_ids:
message_id = field_to_track.message_post(body=f'<strong>{ self._description }:</strong> { self.display_name }').id
trackings = self.env['mail.tracking.value'].sudo().search([('mail_message_id', '=', self.message_ids[0].id)])
for tracking in trackings:
tracking.copy({'mail_message_id': message_id})
If you just want to track on relational and not current model then use write instead of copy method
tracking.write({'mail_message_id': message_id})
And for delete and create you can just use message_post inside create and unlink method
self.message_post(body=f'<strong>{ self._description }:</strong> { self.display_name } created/deleted')

How to create a field for project sale orders

I have fields for a quick access of the entities related to a project. For example:
class project(models.Model):
_name = "project.project"
_description = "Project"
_inherit = 'project.project'
production_order_ids = fields.One2many('mrp.production', 'project_id', 'Production orders')
purchase_order_ids = fields.One2many('purchase.order', 'project_id', 'Purchase orders')
....
I am trying to create a sale_order_ids in the project.project model. My first try did not work:
sale_order_ids = fields.One2many('sale.order', 'project_id', string='Sale orders')
because because the field sale.order.project_id is of type account.analytic.account.
A project.project object inherits from account.analytic.account. This query should work ok if they would share the same Id, but they do not. So the navigation would be:
"project.project".analytic_account_id" -> sale.order".project_id
And the result would be the corresponding sale.order(s).
Use a computed field:
sale_order_ids = fields.One2many('sale.order', compute='_get_sale_orders', string='Sale orders')
#api.model
def _get_sale_orders(self):
for record in self:
record.sale_order_ids = self.env['sale.order'].search([('project_id', '=', record.analytic_account_id.id)]).ids