简体   繁体   English

django中的复合查询-使用额外的,注释的还是原始的sql?

[英]Compound query in django — use extra, annotate, or raw sql?

I need to do the following query: 我需要执行以下查询:

open_positions = Jobs.objects.all()
open_positions_with_apply = open_positions.extra(`has_applied` = 
            JobApplication.objects.filter(profile=profile, job=job).exists()

To clarify what I want to do: 为了澄清我想做什么:

profile = UserProfile.objects.get(id=1)
new_query_set = []
for job in Jobs.objects.all():
    has_applied = JobApplication.objects.filter(job=job, profile=profile)
    new_query_set.append(job with its has_applied value) # in pseudocode, not sure how this would be done

Basically given a profile (the user visiting the page), and a QuerySet of jobs, I need to find a boolean of whether the user has applied. 基本上给定一个配置文件(用户访问该页面)和一个QuerySet作业,我需要查找该用户是否已申请的布尔值。 How would I do this with a django query? 如何使用Django查询做到这一点?

For what it's worth, I went about solving this by getting a job_id values_list of the jobs the user has applied for, and in the template - 对于它的价值,我通过获取用户已申请工作的job_id values_list来解决此问题,该模板位于模板中-

{% for job in jobs %}
    {% if job.id not in jobs_user_has_applied_to %}
    <form>...
    {% else %}
    You've applied for this
{% endfor %}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM