Queying 3 tables in Django

35 views
Skip to first unread message

Oduwa Imade

unread,
May 13, 2023, 5:55:36 PM5/13/23
to Django users
Hello Guys! How do I write a model query in django python to get the User details(Recruitment) when i pass a search parameter( skill) to the Skill table?

class Recruitment(models.Model):
    fname = models.CharField(max_length=50)
    lname = models.CharField(max_length=50)
    address = models.CharField(max_length=100)
    phone = models.IntegerField()
    email = models.CharField(max_length=50)
    password = models.CharField(max_length=30)

class CV(models.Model):
    summary = models.TextField()
    f_key_REC = models.ForeignKey(Recruitment, related_name='cv', on_delete=models.CASCADE)

class Skill(models.Model):
    sk1 = models.CharField(max_length=100, default=None)
    sk2 = models.CharField(max_length=100, default=None)
    sk3 = models.CharField(max_length=100, default=None)
    fk_sCV = models.ForeignKey(CV, related_name='skill', on_delete=models.CASCADE)

_M_A_Y_A_N_K_

unread,
May 13, 2023, 7:19:28 PM5/13/23
to django...@googlegroups.com
You could follow the steps below.
  • Create a view function that takes the search parameter as a keyword argument. 
    • Use the QuerySet object to filter the Skill table by the search parameter. 
    • Use the prefetch_related() method to load the Recruitment objects associated with the Skill objects. 
    • Return the Recruitment objects.
I mean something like this
def get_recruitments_by_skill(request, skill):
    # Get the skills that match the search parameter.
    skills = Skill.objects.filter(sk1=skill)

    # Prefetch the recruitments associated with the skills.
    skills.prefetch_related('recruitment')

    # Return the recruitments.
    return render(request, 'recruitments.html', {'skills': skills})


Thanks & Regards,
---------------------
Mayank Tripathi
"Do what you can, with what you have, where you are -by Theodore Roosevelt"



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c381af8c-e2f7-41b8-ae48-71f389a23459n%40googlegroups.com.

Ryan Nowakowski

unread,
May 17, 2023, 6:51:07 PM5/17/23
to django...@googlegroups.com, Oduwa Imade
skill = Skill.objects.get(...

recruits = Recruitment.objects.filter(cv__skill=skill)

Andrew Fry

unread,
May 17, 2023, 7:04:19 PM5/17/23
to django...@googlegroups.com
Maybe:

from django.db.models import Q

search_parameter = "your_search_parameter"  # Replace with your actual search parameter

recruitments = Recruitment.objects.filter(cv__skill__sk1=search_parameter) | \
              Recruitment.objects.filter(cv__skill__sk2=search_parameter) | \
              Recruitment.objects.filter(cv__skill__sk3=search_parameter)

# Alternatively, you can use Q objects to make the query more concise:
recruitments = Recruitment.objects.filter(
    Q(cv__skill__sk1=search_parameter) |
    Q(cv__skill__sk2=search_parameter) |
    Q(cv__skill__sk3=search_parameter)
)

# Access the recruitment details
for recruitment in recruitments:
    print(recruitment.fname, recruitment.lname, recruitment.address, recruitment.phone, recruitment.email, recruitment.password)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Oduwa Imade

unread,
May 17, 2023, 7:39:54 PM5/17/23
to Django users
Thanks, Andrew. this is super helpful!

Oduwa Imade

unread,
May 17, 2023, 7:42:35 PM5/17/23
to Django users
I want to say thanks to all respondents. I got my code working now. I picked up Python/Django about 3weeks and one of the best decisions I've made so far is joining this community.

Gracias!

Reply all
Reply to author
Forward
0 new messages