gpt4 book ai didi

python - 返回登录用户申请的所有职位的查询集

转载 作者:太空宇宙 更新时间:2023-11-04 09:23:13 25 4
gpt4 key购买 nike

我正在构建一个工作门户,用户可以在其中申请系统上列出的任何工作。假设用户已登录并申请了不同的工作职位,我希望能够返回他申请的所有工作并将其传递到我的模板。我有两个模型:Job 和 Applicants

模型.py

class Job(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
location = models.CharField(choices=country_state, max_length=20)
description = RichTextUploadingField()
requirement = RichTextUploadingField()
years_of_experience = models.IntegerField(blank=True, null=True)
type = models.CharField(choices=JOB_TYPE, max_length=10)
last_date = models.DateTimeField()
created_at = models.DateTimeField(default=timezone.now)
date = models.DateTimeField(default=timezone.now)
filled = models.BooleanField(default=False)

def __str__(self):
return self.title

class Applicants(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
job = models.ForeignKey(Job, on_delete=models.CASCADE, related_name='applicants')
experience = models.IntegerField(blank=True, null=True)
cv = models.FileField(upload_to=user_directory_path)
degree = models.CharField(choices=DEGREE_TYPE, blank=True, max_length=10)
created_at = models.DateTimeField(default=timezone.now)

def __str__(self):
return f'{self.user.get_full_name()} Applied'

在我的 views.py 中,我尝试使用 request.user 获取登录用户并使用 request.user 过滤 Applicants 模型。然后我在应聘职位列表中筛选职位。

View .py

class AppliedJobs(ListView):
model = Applicants
template_name = 'my_job_list.html'
context_object_name = 'jobs'
ordering = ['-date']

@method_decorator(login_required(login_url=reverse_lazy('login')))
def dispatch(self, request, *args, **kwargs):
return super().dispatch(self.request, *args, **kwargs)

def get_queryset(self):
user = self.request.user
my_job_list = Applicants.objects.filter(user=user).values_list('job', flat=True)
return Applicants.objects.filter(job__in=my_job_list)

我不知道这是否是最好的方法。它返回工作,但不是它应该的方式。我需要它来仅列出用户申请的工作。

模板.html

      {% for job in jobs %}
<ul class="job-listings mb-5">
<li class="job-listing d-block d-sm-flex pb-3 pb-sm-0 align-items-center">
<a href="{% url 'job-detail' job.id %}"></a>
<div class="job-listing-logo">
<img src="{{ job.user.profile.image.url }}" alt="Free Website Template by Free-Template.co" class="img-fluid">
</div>

<div class="job-listing-about d-sm-flex custom-width w-100 justify-content-between mx-4">
<div class="job-listing-position custom-width w-50 mb-3 mb-sm-0">
<h4>{{ job.title }}</h4>
</div>
<div class="job-listing-location mb-3 mb-sm-0 custom-width w-25">
<i class="fa fa-map-marker-alt"></i>{{ job.location }}
</div>
<div class="job-listing-meta">
{% if job.type == 'Full time' %}
<span class="badge featured-badge badge-success">Full time</span>
{% elif job.type == 'Part time' %}
<span class="badge featured-badge badge-primary">Part time</span>
{% elif job.type == 'Internship' %}
<span class="badge featured-badge badge-warning">Internship</span>
{% else %}
<span style="color: #ffffff;" class="badge featured-badge badge-warning">Contract</span>
{% endif %}
</div>
</div>

</li>
</ul>
{% endfor %}

最佳答案

my_job_list 已经是您的用户的正确 Applicants 对象列表。通过在末尾添加 values_list('job'),您只是创建了一个职位 ID 列表,而按这些值过滤的最后一行实际上添加了其他用户对相同职位的所有申请,这不是您想要的。

所以你只需要做:

def get_queryset(self):
return Applicants.objects.filter(user=self.request.user).distinct('job').select_related('job')

我添加了 select_related 以便在同一查询中获取关联的 Job 行,并且,仅对于 PostgreSQL,distinct 到确保每个工作只获取一个应用程序(以防用户多次申请同一个工作)

在您的模板中,您可以循环遍历应用程序:

{% for application in applications %}
{{ application.job.title }}
{% endfor %}

您也可以通过直接查询 Job 来获取职位列表,但这意味着您的模板中不会包含申请详细信息:

Job.objects.filter(applicants__user=user).distinct()

关于python - 返回登录用户申请的所有职位的查询集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59049713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com