Home > Mobile >  compare objects in views.py of 2 different models objects in Django
compare objects in views.py of 2 different models objects in Django

Time:10-22

I am trying to compare 2 objects (skills of a job posting such as web development, marketing, etc to the same skills of a logged-in user), if matched will display that job posting.

The goal is to display multiple job postings that match the user. Currently, jobmatch.html does not display any jobs.

Thank you for taking the time!!

views.py

from apps.job.models import Job
from apps.userprofile.models import User_profile

def match_jobs(request):
    match_jobs = {}
    for job in Job.objects.all():
        if job.product_management == User_profile.product_management:
            match_jobs['product management'] = job
        elif job.web_development == User_profile.web_development:
            match_jobs['web development'] = job
        elif job.user_experience == User_profile.user_experience:
            match_jobs['user experience'] = job
        elif job.marketing == User_profile.marketing:
            match_jobs['marketing'] = job
        elif job.finance == User_profile.finance:
            match_jobs['finance'] = job

    return render(request, 'jobmatch.html', match_jobs)

Job.models.py

class Job(models.Model):
    title = models.CharField(max_length=255)    
    location = models.CharField(max_length=255, blank=True, null=True)
    description = models.TextField()
    requirements = models.TextField(blank=True, null=True)
    product_management = models.BooleanField(default=False)
    web_development = models.BooleanField(default=False)
    user_experience = models.BooleanField(default=False)
    marketing = models.BooleanField(default=False)
    finance = models.BooleanField(default=False)

User.models.py

class User_profile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile', on_delete=models.CASCADE)
    is_employer = models.BooleanField(default=False)
    resume = models.ImageField(default='default.jpg', upload_to='resume')
    full_name = models.CharField(max_length=255, default='Enter full name')
    relevant_background = models.TextField()
    product_management = models.BooleanField(default=False)
    web_development = models.BooleanField(default=False)
    user_experience = models.BooleanField(default=False)
    marketing = models.BooleanField(default=False)
    finance = models.BooleanField(default=False)

jobmatch.html

            {% for job in match_jobs %}
                <div class="colum is-4">
                    <div class="card">
                        <div class="card-content">
                            <div class="media">
                                <div class="media-content">
                                    <p class="title is-4">{{ job.title }}</p>
                                    <p class="subtitle is-6">{{ job.created_at }}</p>
                                </div>
                            </div>

                            <div class="content">
                                {{ job.location }}

                                <br>

                                <a href="{% url 'job_detail' job.id %}">View</a>
                            </div>
                        </div>
                    </div>
                </div>
            {% endfor %}

CodePudding user response:

You can create a Skill model to create any skills you want and also relate them to jobs and users using ManyToManyField. This will make it easy to add, update or remove any skills.

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

class Job(models.Model):
    title = models.CharField(max_length=255)    
    location = models.CharField(max_length=255, blank=True, null=True)
    description = models.TextField()
    requirements = models.TextField(blank=True, null=True)
    skills = models.ManyToManyField(Skill, blank=True, related_name='as_job_skill')

class User_profile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile', on_delete=models.CASCADE)
    is_employer = models.BooleanField(default=False)
    resume = models.ImageField(default='default.jpg', upload_to='resume')
    full_name = models.CharField(max_length=255, default='Enter full name')
    relevant_background = models.TextField()
    skills = models.ManyToManyField(Skill, blank=True, related_name='as_user_skill')

You can also find the matching jobs for users very easily.

def match_jobs(request):
    user_profile = request.user.userprofile
    matched_jobs = Job.objects.filter(skills__in=user_profile.skills.all()).distinct()
    return render(request, 'jobmatch.html', matched_jobs)

CodePudding user response:

You need to redesign your models.

Store all the skills inside a separate table.

class Skill(models.Model):
  name = models.CharField(max_length=50) # example web development

Now you can link your job and user profile model with Skill model through ManyToMany relationship.

class Job(models.Model):
  ......
  skills = models.ManyToManyField(Skill)
 
class UserProfile(models.Model):
.........
  skills = models.ManyToManyField(Skill)

Now you can get matched jobs with this.

 user_skills = request.user.userprofile.skills.all()
 matched_jobs = Job.objects.filter(skills__in=user_skills)

CodePudding user response:

You have iteration by dict keys now. You have to do something like: {% for job in match_jobs.values %} or {% for job_identifier, job in match_jobs.items %}

  • Related