[Answered ]-Django add custom permission

1πŸ‘

βœ…

In the admin panel, I created 2 groups, respectively, and also included users in these groups.

Query these group names by filtering request.user.groups with name__in. Then filter Employee.objects by those results with dept__in.

Note that this assumes the admin group names are named exactly like the Employee.dept field. If not, rename the admin groups to match the Employee.dept field (including capitalization, spaces, etc.).

def employee_list(request):
    groups = [g.name for g in request.user.groups.filter(name__in=['Group1', 'Group2')]
    context = {'employee_list': Employee.objects.filter(dept__in=groups)}

    return render(request, 'employee_register/employee_list.html', context)
πŸ‘€tdy

0πŸ‘

Try this:

context = {'employee_list': Employee.objects.filter(dept=request.user.group)}

I’m not sure how you made the User belong in one of the groups but I’m guessing you also have a group field on your User model.

Leave a comment