Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
7987 Lượt xem

Hi,

I'm using odoo 12 and I have a model called "shift", this shift is related to user (user_id), and has a selection field called status with options: (open, closed)

i want to ensure that user can't open 2 shifts at the same time

he can have a lot of closed shifts but if he has one open shift he can't open another one

anyone can help me with creating the constraint and i'll be thankful 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You can apply constrains on two fields-

@api.constrains('user_id', 'status')

def check_user_open_shift(self):

    // here you can search already existing shift in open state for selected user and raise error if already exist.


 


Ảnh đại diện
Huỷ bỏ
Tác giả

Thank You

Hello sir i want to use constrain on many2one field, the constrain should be different for each data of that many2one field.

Câu trả lời hay nhất

Try to add to the shift model like this:

from odoo.exceptions import UserError # in the import list
@api.multi
@api.constrains('state', 'user_id')
def _check_open_shifts(self):
user_ids = self.mapped("user_id") # assume that shift user field is called 'user_id'
for user in user_ids:
open_shifts = self.search([("state", "=", "open"), ("user_id", "=", user.id)], limit=2)
if len(open_shifts) > 1:
raise UserError("Only a single open shift is allowed per user")
Ảnh đại diện
Huỷ bỏ
Tác giả

sorry but in this way we will prevent open 2 shifts whoever the user was, what i want is to prevent opening 2 shifts for the same user, i mean user a can have one open shift and user b can have one open shift etc but user a can't have 2 open shift at the same time

look at the updated answer

Tác giả

Thank you very much, i appreciate your help

Câu trả lời hay nhất

Hi,

You can check how the warning is shown in the attendance module. In it if the employee is checkd in once, on next checkin(without checkout)  and system will throw a warning message .


Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you

Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 5 24
7175
1
thg 7 23
2307
3
thg 6 23
3213
4
thg 6 22
4935
3
thg 4 22
5509