I'm beginner in django and python. I have models :
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}"
In views.py, i have '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
This function get the job_id and need to find the best candidates for this job : first by matching between the job title to the employee title (for ex' : software developer), and then matching between candidate's skills to job's required skiils .
I think that my function is inefficient. Someone has any idea how to write it in efficient way?