我们如何将用户重定向到 Django 中的指定管理员?

How can we redirect users to specified admin in Django?

我正在开发休假管理系统,其中有几个用户向特定经理报告(管理员就 django 而言,假设是否有多个管理员)。

from django.db import models
from django.contrib.auth.models import User

CHOICES = (('1','Earned Leave'),('2','Casual Leave'),('3','Sick Leave'),('4','Paid Leave'))

class Leave(models.Model):

    name = models.CharField(max_length = 50)
    user = models.ForeignKey(User, on_delete = models.CASCADE, null =True)
    employee_ID = models.CharField(max_length = 20)
    department = models.CharField(max_length = 15)
    designation = models.CharField(max_length = 15)
    type_of_leave = models.CharField(max_length = 15, choices = CHOICES, default = None)
    from_date = models.DateField(help_text = 'mm/dd/yy')
    to_date = models.DateField(help_text = 'mm/dd/yy')
    reporting_manager = models.CharField(max_length = 50, default = None)
    reason = models.CharField(max_length= 180)
    accepted = models.BooleanField(('approval status'), default=False)


    def __str__(self):
         return self.name

我试过使用循环和比较 reporting_manager 列来调整我的 admin.py,但我无法达到我的目标。我什至尝试使用内置的用户权限,但这对我没有帮助。简而言之,如果有超过 admin 的名字。 data/form 应该只对那些在提交给用户的表单中提到其姓名的管理员可见。

我不太了解模型及其字段。无论如何,您应该重写 AdminModel 中的 get_queryset 函数,并像这样过滤查询集

class LeaveAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)

        #### return everything if the admin is superuser
        if request.user.is_superuser:
            return qs

        ###else do the filtering here based on the staff name [username or anything else]
        return qs.filter(reporting_manager=request.user.username)