Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
1430 Visualizzazioni

Hi,


class EmployeeTravelReqest(models.Model):
_name = 'hr.emp.travel.request'
_description = "Travel Request"
_inherit = ['mail.thread', 'mail.activity.mixin']

@api.multi
def hr_manager_user_ids_get(self):
self.ensure_one()
hr_group_ids = self.env.ref('hr.group_hr_manager').users.ids
self.hr_manager_user_ids = [(6, 0, hr_group_ids)]

user_id = fields.Many2one('res.users')
hr_manager_user_ids = fields.Many2many('res.users', compute="hr_manager_user_ids_get")

name = fields.Char(string="Name")
employee_id = fields.Many2one('hr.employee', string="Employee")
# related fields
department_id = fields.Many2one(related="employee_id.department_id", store=True)
company_id = fields.Many2one(related="employee_id.company_id", store=True)
parent_id = fields.Many2one(related="employee_id.parent_id", store=True)
job_id = fields.Many2one(related="employee_id.job_id", store=True)
grade_id = fields.Many2one(related="employee_id.grade_id", store=True)
currency_id = fields.Many2one(related="employee_id.company_id.currency_id", store=True)
# Travel Dates
from_date = fields.Date(string='From Date')
to_date = fields.Date(string='To Date')
expect_travel_days = fields.Integer(string="Days", compute="compute_days")
state = fields.Selection(
[('draft', 'Draft'), ('waiting', 'Waiting for confirmation'), ('confirmed', 'Confirmed'), ('approved', 'Approved'),
('closed', 'Closed'), ('rejected', 'Rejected'), ('canceled', 'Canceled')], default="draft", track_visibility="onchange")
# travel locations
travel_location_ids = fields.One2many('hr.emp.travel.location', 'travel_request_id_ref', required=True)
total_travel_days = fields.Integer(string="Days", compute="compute_days_travel_location")
# Expenses(proposed/actual)
expense_amount_ids = fields.One2many('hr.expense.amount', 'travel_request_id')
total_expense_amount = fields.Monetary(string="Grand Total", compute="compute_total_expense_amount", store=False)
ticket_book = fields.Boolean(string="Company Books ticket for You", track_visibility="onchange")
# Log
request_by = fields.Many2one('res.users', default=lambda self: self.env.uid)
approved_by = fields.Many2one('res.users')
rejected_by = fields.Many2one('res.users')
request_date = fields.Date(string="Requested Date", default=date.today())
approved_date = fields.Date()
rejected_date = fields.Date()
reject_reason = fields.Char("Reason For Reject")
# Reason
reason = fields.Text(string="Reason")
# Currency
currency_ids = fields.One2many('hr.travel.currency', 'travel_request_id')

# Expenses expenses_ids = fields.One2many('hr.expense', 'travel_request_id_ref'
@api.multi
def action_send_manager(self):
for rec in self:
if not rec.travel_location_ids:
raise Warning("Please enter the travel information before submit to manager")
else:
rec.state = 'waiting'

I get this error when I try to save more than one line in one2many field.


Thank you in advance for any help.



Avatar
Abbandona

Instead of sharing whole code, please check log and see which section throws the error, and please share it.
To understand single ton error in odoo and to learn its solution see: https://www.youtube.com/watch?v=Rv44nFVn_5U

you can check with one2many class(hr.emp.travel.location) like function, search, domain etc.

Risposta migliore

Hi,

Singleton error occurs because somewhere in your code, a method that expects one record (self.ensure_one() is called) is being passed multiple records. In your case, it is happening with the One2many travel_location_ids when you try to save multiple lines.To fix this, avoid using ensure_one() in compute methods or methods that can be called on multiple records, and always loop over self when accessing fields like One2many or Many2many. For example, in the compute_days_travel_location method, you should iterate over each record in self and calculate the total travel days individually. Similarly, in the hr_manager_user_ids_get compute method, remove ensure_one() and loop over self to assign the HR manager users for each record. Also, make sure any button or action methods that work on multiple records, such as action_send_manager, use a loop to handle each record properly. Finally, review all methods that access One2many or Many2many fields to ensure they handle multiple records without assuming a single record. Following these steps will prevent singleton errors when saving multiple lines in the travel_location_ids field.


Hope it helps.

Avatar
Abbandona
Risposta migliore

The error usually happens when a method is written with ensure_one() but is called on multiple records at once, which leads to the singleton issue. You can fix it by adjusting the method to handle recordsets, or by making sure the compute field only runs on a single record at a time. Reviewing how the travel_location_ids field is triggered can help narrow it down. It’s a bit like planning reliable operations such as sallimoservice.com, where handling multiple requests smoothly requires structuring the process correctly. In this case, refining the method logic should resolve the conflict.

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
ott 23
9657
1
set 23
3686
1
mag 23
2609
2
apr 23
3274
1
mar 23
2564