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

时间:2018-08-16 13:07:54

标签: python django admin user-permissions

我正在使用请假管理系统,其中有几个用户向特定的经理报告(以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

my application panel

我已经尝试通过使用循环并比较report_manager列来调整admin.py,但是我无法达到目标。我什至尝试使用内置的用户权限,但这对我没有帮助。简而言之,如果他们的名字超过admin。数据/表单仅对那些在用户提交的表单中提及其姓名的管理员可见。

1 个答案:

答案 0 :(得分:0)

我不太了解模型及其领域。无论如何,您应该覆盖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)