比较 Django 中 2 个不同模型对象的 views.py 中的对象
compare objects in views.py of 2 different models objects in Django
我正在尝试比较 2 个对象(Web 开发、市场营销等职位发布的技能与登录用户的相同技能),如果匹配,将显示该职位发布。
目标是显示与用户匹配的多个职位发布。当前,jobmatch.html 不显示任何职位。
感谢您抽出宝贵时间!!
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 %}
您现在可以通过字典键进行迭代。您必须执行以下操作:{% for job in match_jobs.values %}
或 {% for job_identifier, job in match_jobs.items %}
您可以创建一个 Skill
模型来创建您想要的任何技能,并将它们与使用 ManyToManyField
的工作和用户相关联。这将使添加、更新或删除任何技能变得容易。
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')
您还可以非常方便地为用户找到匹配的职位。
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)
您需要重新设计模型。
将所有技能存储在单独的 table 中。
class Skill(models.Model):
name = models.CharField(max_length=50) # example web development
现在您可以通过多对多关系link您的工作和用户配置文件模型与技能模型。
class Job(models.Model):
......
skills = models.ManyToManyField(Skill)
class UserProfile(models.Model):
.........
skills = models.ManyToManyField(Skill)
现在你可以用这个找到匹配的工作。
user_skills = request.user.userprofile.skills.all()
matched_jobs = Job.objects.filter(skills__in=user_skills)
我正在尝试比较 2 个对象(Web 开发、市场营销等职位发布的技能与登录用户的相同技能),如果匹配,将显示该职位发布。
目标是显示与用户匹配的多个职位发布。当前,jobmatch.html 不显示任何职位。
感谢您抽出宝贵时间!!
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 %}
您现在可以通过字典键进行迭代。您必须执行以下操作:{% for job in match_jobs.values %}
或 {% for job_identifier, job in match_jobs.items %}
您可以创建一个 Skill
模型来创建您想要的任何技能,并将它们与使用 ManyToManyField
的工作和用户相关联。这将使添加、更新或删除任何技能变得容易。
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')
您还可以非常方便地为用户找到匹配的职位。
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)
您需要重新设计模型。
将所有技能存储在单独的 table 中。
class Skill(models.Model):
name = models.CharField(max_length=50) # example web development
现在您可以通过多对多关系link您的工作和用户配置文件模型与技能模型。
class Job(models.Model):
......
skills = models.ManyToManyField(Skill)
class UserProfile(models.Model):
.........
skills = models.ManyToManyField(Skill)
现在你可以用这个找到匹配的工作。
user_skills = request.user.userprofile.skills.all()
matched_jobs = Job.objects.filter(skills__in=user_skills)