Created
July 19, 2018 20:03
-
-
Save rg3915/54f7563771506521693bd8c492298755 to your computer and use it in GitHub Desktop.
Fazendo busca no Django com dicionário
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fazendo busca no Django com dicionário | |
company = Company.objects.get(name='MyCompany') | |
company_cands = CompanyCandidate.objects.select_related('candidate')\ | |
.filter(company=company) | |
company_cand_list = company_cands.values_list('candidate', flat=True) | |
candidates = Candidate.objects.select_related('job', 'candidate')\ | |
.filter(candidate__in=company_cand_list)\ | |
.annotate(Max('apply_date')) | |
# Cria um dicionário | |
cand_dict = {} | |
for cand in candidates: | |
cand_dict[cand.candidate.pk] = cand.job.title | |
for cand in company_cands: | |
last_job = None | |
# Faz a busca dos candidatos pelo dicionário. | |
if cand.candidate.pk in cand_dict: | |
last_job = cand_dict[cand.candidate.pk] | |
if last_job: | |
print(cand.candidate.get_full_name(), last_job) | |
else: | |
print(cand.candidate.get_full_name(), '---') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment