社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

比较视图中的对象。Django中两个不同模型对象的py

dt123 • 3 年前 • 1316 次点击  

我试着比较两个对象(一个职位发布的技能,如网络开发、营销等,与登录用户的相同技能),如果匹配,将显示该职位发布。

目标是显示多个与用户匹配的职位公告。目前是jobmatch。html不显示任何作业。

谢谢你抽出时间!!

意见。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)

工作模型。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)

使用者模型。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 %}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129769
文章 [ 3 ]  |  最新文章 3 年前
LMA9
Reply   •   1 楼
LMA9    4 年前

你现在有了dict键的迭代。你必须做一些事情,比如: {% for job in match_jobs.values %} {% for job_identifier, job in match_jobs.items %}

Ajay Lingayat
Reply   •   2 楼
Ajay Lingayat    4 年前

你可以创建一个 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)
arjun
Reply   •   3 楼
arjun    4 年前

你需要重新设计你的模型。

将所有技能存储在单独的表格中。

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

现在,您可以通过多种关系将您的工作和用户配置文件模型与技能模型联系起来。

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)