简体   繁体   English

如何检查一个人是否包含在模型中以进行工作/发布

[英]How can I check whether a person is included in a model to make a Job/Post

I have created a School System-like system, that creates a job and sends them to employees/users.我创建了一个类似学校系统的系统,该系统创建了一份工作并将其发送给员工/用户。 I'm almost done making this system however I can't seem to know what do to check if the user is included in the manager model that I created to create a job.我几乎完成了这个系统的制作,但是我似乎不知道如何检查用户是否包含在我为创建工作而创建的管理器模型中。

Also, how can a user just see all their job that was assigned to them.此外,用户如何才能看到分配给他们的所有工作。 All I know is to use objects.all but that might only seem to show all of the jobs that was posted, I just want the user to see the job included to them.我所知道的就是使用objects.all但这似乎只显示发布的所有工作,我只是希望用户看到包含在他们中的工作。

Here is my model.py:这是我的model.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.OneToOneField(Manager, on_delete=models.CASCADE)
    member = models.OneToOneField(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

And Views.py:和 Views.py:

from django.shortcuts import render
from django.views.generic import ListView, CreateView
from .models import Job
from profiles.models import User
# Create your views here.

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


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

How can I proceed?我该如何继续?

Use get_queryset to filter job by user使用get_queryset按用户过滤作业

Ex:前任:

class jobs(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查以cron作业开头的脚本是否完成? - How can I check whether the script started with cron job is completed? 如何为每个人发出邀请? - How can I make an invitation for each person? 如何检查post_save中的模型字段是否已更改? - How can i check if model field was changed in post_save? 如何检查距模型对象创建时间是否超过10分钟? - How can I check whether time from model object created time is over 10mins? 如何在 Django 中创建一个消息模型,您可以在其中选择多个人作为收件人 - how to make a message model in Django where you can choose more than one person as the recipient 如何将 model 字段的值分配给一个变量,它应该属于登录的人 - how do I make a model field's value be assigned to a variable and it should belong to the person logged in 如何使用 `urlparse` 检查 URL 是否有效? - How can I check whether a URL is valid using `urlparse`? 如何检查 Python object 是否满足类型约束? - How can I check whether a Python object satisfies a type constraint? 如何检查 scipy 分布是否是离散的? - how can I check whether a scipy distribution is discrete? 如何检查给定文件是否为FASTA? - How can I check whether a given file is FASTA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM