Django:如何在经理模型中添加经理

时间:2019-05-07 08:59:52

标签: django django-models django-templates django-views

here is the image我是django的新手,正在从事一个项目,管理员可以将团队分配给经理。我想知道我该如何附加那个模型和经理。我的意思是经理是具有is_staff状态为true并具有我为经理授予的某些特殊权限的人。并且只有这些人的名字应该显示给i_staff状态为true的管理员,并具有该特殊权限。而且我不知道该怎么办。

这是经理和视图的模型。

def login(request):
if request.method =='POST':
    # print(request.POST)
    username = request.POST['username']
    password = request.POST['password']

    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        print (user.has_perm('app.edit_task'))
        # return redirect('index')

        if user.is_superuser:
            return redirect('master')

        # elif user.has_perm('app.edit_task'):
        #     return redirect('manager')
        elif user.is_staff:
            return redirect('manager')
        else:
            return redirect('index')

    else:
        messages.error(request, 'Invalid Credentials')
        return redirect('login')    
else: 
    return render(request, 'vadash/sign-in.html')

这是我的经理模式

class manager(models.Model):
name = models.CharField(max_length= 500)
designation = models.CharField(max_length= 500)
class Meta:
    permissions = [
        ("edit_task", "can edit the task"),
    ]

def __str__(self):
    return self.name

class Create_Team(models.Model):
first_name = models.CharField(max_length= 50)
last_name = models.CharField(max_length= 50)
company_name = models.CharField(max_length= 100)
address = models.CharField(max_length= 1000)
state = models.CharField(max_length= 100)
city = models.CharField(max_length= 100)
pin_code = models.CharField(max_length= 100)
email = models.CharField(max_length= 100)
phone_number = models.CharField(max_length= 20)
company_description = models.TextField()
work_description = models.TextField()
team_member = models.CharField(max_length= 1000)
remark = models.TextField(blank=True)
status = models.CharField(max_length= 30)
managers = models.ForeignKey('manager', on_delete = models.SET_NULL, null=True)

这是管理控制台,管理员可以在其中执行某些操作:

 def admin_team_detail(request):
   obj= Create_Team.objects.all()
  print(request.method)
  if request.method == 'POST':
# First, you should retrieve the team instance you want to update
    team = Create_Team.objects.get(id=request.POST.get('id'))
    print(request.POST)
    # Next, you update the status
    # if request.POST.get('accept'):
    #     team.status = request.POST.get('accept')
    #     team.save()

    # else:
    #     team.status = request.POST.get('reject')
    #     team.save()

    if request.POST.get('status'):
        team.status = request.POST.get('status')
        team.save()
return render(request, "admin/team-details.html", {"object": obj})


 def accept(request):
  obj= Create_Team.objects.filter(status='Accept') 
 managers = manager.objects.all()
 if request.method == 'POST':
   createteam = Create_Team()
   createteam_id = int(request.POST.get('team', 1))
   manager_id = int(request.POST.get('manager', 1))
   createteam = Create_Team.objects.get(pk=createteam_id)
   createteam.manager = manager.objects.get(pk=manager_id)
   createteam.save()

  return render(request, "admin/accept.html", {"object": obj,  "managers": managers})

def reject(request):
 obj= Create_Team.objects.filter(status='Reject') 
 return render(request, "admin/reject.html", {"object": obj})

我希望具有is_staff状态为true且仅具有该权限的用户才应该在管理员中显示。

0 个答案:

没有答案