检查用户是否在管理员栏中以创建职位/任务

问题描述 投票:0回答:1

我正在DJango中学习,并且从文档以及StackOverflow中学习了很多东西。现在,我有点困惑,我只想知道如果用户在工作模型的经理列中/也可以在经理模型中,也可以在基于类的视图中检查谁?

我尝试使用UserPassesTestMixin来检查用户是否属于其中,但出现Generic detail view createjob must be called with either an object pk or a slug in the URLconf.错误

我只需要有人将我指向正确的方向或给我提示。我也尝试过,这:

class createjob (LoginRequiredMixin,CreateView):
    model = Job
    fields = ['member','title', 'description', 'file']

    def form_valid(self,form):
        form.instance.manager=self.request.user
        return super().form_valid(form)

但是它给我一个Cannot assign "<SimpleLazyObject: <User: edlabra>>": "Job.manager" must be a "Manager" instance.的错误

这是我的views.py:

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import ListView, CreateView
from .models import Job, Member
from profiles.models import User
from django.contrib.auth.decorators import login_required
# Create your views here.


class jobs(LoginRequiredMixin,ListView):
    model = Job
    template_name = 'users/user_jobs.html'
    context_object_name = 'jobs'

    def get_queryset(self):
        return Job.objects.filter(member__member=self.request.user)


class createdjobs(LoginRequiredMixin,ListView):
    model = Job
    template_name = 'users/manager_jobs.html'
    context_object_name = 'jobs'

    def get_queryset(self):
        return Job.objects.filter(manager__manager=self.request.user)


class teamview(LoginRequiredMixin,ListView):
    model = Member
    template_name = 'users/manage_team.html'
    context_object_name = 'members'

    def get_queryset(self):
        return Member.objects.filter(manager__manager=self.request.user)



class createjob (LoginRequiredMixin,UserPassesTestMixin,CreateView):
    model = Job
    fields = ['member','title', 'description', 'file']

    def test_func(self):
        job=self.get_object()
        if self.request.user == Job.manager:
            return True
        return False

Models.py:

from django.db import models
from profiles.models import User
# Create your models here.

class Points (models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    points = models.IntegerField(default=0, null=False)

    def __str__(self):
        return self.user.username

class Profile (models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.png',upload_to='profile_pics')


    def __str__(self):
        return f'{self.user.username}Profile'


class Manager (models.Model):
    name = models.CharField(max_length=30, blank=True, null=True)
    manager = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Member (models.Model):
    name = models.CharField(max_length=30, blank=True, null=True)
    manager = models.ForeignKey(Manager, on_delete=models.CASCADE)
    member = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Job (models.Model):
    manager = models.ForeignKey(Manager, on_delete=models.CASCADE)
    member = models.ForeignKey(Member, on_delete=models.CASCADE)
    title = models.CharField(max_length=30, blank=False, null=False)
    description = models.TextField()
    datePosted = models.DateTimeField (auto_now = True)
    file = models.FileField(null=True, blank=True,upload_to='job_files')

    def __str__(self):
        return self.title
python django django-views mixins
1个回答
0
投票

从管理员表中分配用户。

def form_valid(self,form):
        form.instance.manager=Manager.objects.get(manager=self.request.user)
        return super().form_valid(form)
© www.soinside.com 2019 - 2024. All rights reserved.