簡體   English   中英

以有效的方式找到最佳候選人,Django

[英]Find best candidates in efficient way, Django

我是 django 和 python 的初學者。 我有模型:

 class Employee(models.Model):
    full_name = models.CharField(max_length = 64)
    title = models.CharField(max_length = 64)

    def __str__(self):
        return f"{self.full_name} ( {self.title} )"

class Skill(models.Model):
    name = models.CharField(max_length = 64)

    def __str__(self):
        return f"{self.name}"

class Candidate(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name="employee")
    skill = models.ForeignKey(Skill, on_delete=models.CASCADE, related_name="skill")

    def __str__(self):
        return f"{self.id}: {self.employee} knows - {self.skill}"

class Job(models.Model):
    title = models.CharField(max_length = 64)
    skills = models.ManyToManyField(Skill, blank=True, related_name="Jobs")

    def __str__(self):
        return f"{self.title}"

在views.py中,我有'finder' function:

def finder(job_id):
    job = Job.objects.get(id=job_id) # get the specific job
    relevant_candidates = [] # all the relevant candidates of this kob
    common = [] # number of common skills between the employee_skill and the 
    relevant_employees_by_title = Employee.objects.filter(title = job.title) # first filter the candidates by the job title 
    job_skills = []
    for skill in job.skills.all():
        print(skill.id)
        job_skills.append(skill.id)

    for employee in relevant_employees_by_title: 
        employee_skills =[]
        candidateCorrect = Candidate.objects.filter(employee__id = employee.id).values_list('skill', flat=True)
        for skill in candidateCorrect:
            employee_skills.append(skill)

        common_skills = list(set(job_skills) & set(employee_skills))
        
        if (len(common_skills)>0): #if there are common skills
            relevant_candidates.append(employee) 
            common.append(len(common_skills))

    candidates = zip(relevant_candidates,common)
    candidates = sorted(candidates,key = lambda t: t[1], reverse = True) # sort the candidates by the number of common skiils , descending order
    candidates = candidates[:50] # Select the best 50 candidates

    return candidates

此 function 獲取 job_id 並需要找到該職位的最佳候選人:首先通過職位與員工職位的匹配(例如:軟件開發人員),然后將候選人的技能與職位所需的技能進行匹配。

我認為我的 function效率低下 有人知道如何以有效的方式編寫它嗎?

第一個Candidate不是正確的命名,將其重命名更有意義。

您所需要的只是分組; 獲得the skills后。 按員工查詢Candidate count組,其中技能中the skills按計數排序,然后限制。

這里是組查詢的例子

Candidate.objects.values('employee').annotate(dcount=Count('employee')
# 🎉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM