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.
π€Toni SredanoviΔ
- [Answered ]-How to print first 100 character in django?
- [Answered ]-Django, define field to be hidden in every form
- [Answered ]-How to redirect back two pages Django?
- [Answered ]-How to store data from html form to postgres database using Django 1.10?
- [Answered ]-Aldryn β how to add own Django apps and its dependencies
Source:stackexchange.com