如何在 Django 中执行查询两个多对多字段?
How to execute query two many to many field in Django?
#我是 python 网络框架 Django 的新手。
#models.py
class User(AbstractUser):
is_employeer = models.BooleanField(default=False)
is_jobseeker = models.BooleanField(default=False)
class allskill(models.Model):
skills = models.CharField(max_length=200, blank=True, null=True)
def __str__(self):
return self.skills
def get_absolute_url(self):
return reverse('employee:post_job')
class jobseeker(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
phone_number = models.CharField(max_length=20)
location = models.CharField(max_length=100)
gender = models.CharField(max_length=20, choices=GENDER_CHOICE, default=GENDER_MALE)
email = models.EmailField(default='ajay@gmail.com')
jobseeker_skill = models.ManyToManyField(allskill)
def __str__(self):
return self.user.username
class Employeer(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
company_name = models.CharField(max_length=50, default='company name')
company_tel = models.CharField(max_length=20, default='01-56559565')
phone_number = models.CharField(max_length=20)
location = models.CharField(max_length=100)
company_website = models.URLField(default="company.com")
company_email = models.EmailField(default='company@gmail.com')
conpany_type = models.CharField(max_length=200, choices=COMPANY_TYPE, default=TE)
def __str__(self):
return self.user.username
class Job_Post(models.Model):
job_title = models.CharField(max_length=200)
job_descreption = models.TextField()
user = models.ForeignKey(User, on_delete = models.CASCADE)
job_post_date = models.DateField(default=timezone.now)
required_skill = models.ManyToManyField(allskill)
time = models.CharField(max_length=200, choices=time, default=FT)
def __str__(self):
return f"{self.user.username}'s {self.job_title}"
def get_absolute_url(self):
return reverse('employee:job_list', kwargs={'username':self.user})
#views.py
class joblist(ListView):
model = Job_Post
template_name = 'my_skill.html'
context_object_name = 'jobsforjobseekerskill'
def get_queryset(self):
jobseeker = self.request.user.is_jobseeker
jobseeker_skills = Job_Post.objects.filter(required_skill__pk__in = jobseeker.jobseeker_skill.values_list('pk', flat = True))
print(jobseeker_skills)
return jobseeker_skills
#得到这个错误
enter image description here
问题
写代码的方式好吗?
如何
以求职者身份登录后,求职者可以显示仅与其技能相关的职位。如何做到这一点?
写代码的正确方式是什么?
您可以这样修改代码:
def get_queryset(self):
jobseeker = self.request.user.jobseeker # as there is OneToOne relation between user and jobseeker
# FYI please use PascalCase when defining name
jobseeker_skills = Job_Post.objects.filter(required_skill__pk__in = jobseeker.jobseeker_skill.values_list('pk', flat = True))
print(jobseeker_skills)
return jobseeker_skills
您只需在代码中将 is_jobseeker
与 jobseeker
相关联即可。
一个更干净的解决方案可以是这样的:
def get_queryset(self):
return Job_Post.objects.filter(required_skill__jobseeker__user=self.request.user)
我在 allpost
和 jobseeker
之间使用 reverse relation 以及 Job_Post
和 all_post
之间的关系来获取 Job_Post
查询集。
#我是 python 网络框架 Django 的新手。 #models.py
class User(AbstractUser):
is_employeer = models.BooleanField(default=False)
is_jobseeker = models.BooleanField(default=False)
class allskill(models.Model):
skills = models.CharField(max_length=200, blank=True, null=True)
def __str__(self):
return self.skills
def get_absolute_url(self):
return reverse('employee:post_job')
class jobseeker(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
phone_number = models.CharField(max_length=20)
location = models.CharField(max_length=100)
gender = models.CharField(max_length=20, choices=GENDER_CHOICE, default=GENDER_MALE)
email = models.EmailField(default='ajay@gmail.com')
jobseeker_skill = models.ManyToManyField(allskill)
def __str__(self):
return self.user.username
class Employeer(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
company_name = models.CharField(max_length=50, default='company name')
company_tel = models.CharField(max_length=20, default='01-56559565')
phone_number = models.CharField(max_length=20)
location = models.CharField(max_length=100)
company_website = models.URLField(default="company.com")
company_email = models.EmailField(default='company@gmail.com')
conpany_type = models.CharField(max_length=200, choices=COMPANY_TYPE, default=TE)
def __str__(self):
return self.user.username
class Job_Post(models.Model):
job_title = models.CharField(max_length=200)
job_descreption = models.TextField()
user = models.ForeignKey(User, on_delete = models.CASCADE)
job_post_date = models.DateField(default=timezone.now)
required_skill = models.ManyToManyField(allskill)
time = models.CharField(max_length=200, choices=time, default=FT)
def __str__(self):
return f"{self.user.username}'s {self.job_title}"
def get_absolute_url(self):
return reverse('employee:job_list', kwargs={'username':self.user})
#views.py
class joblist(ListView):
model = Job_Post
template_name = 'my_skill.html'
context_object_name = 'jobsforjobseekerskill'
def get_queryset(self):
jobseeker = self.request.user.is_jobseeker
jobseeker_skills = Job_Post.objects.filter(required_skill__pk__in = jobseeker.jobseeker_skill.values_list('pk', flat = True))
print(jobseeker_skills)
return jobseeker_skills
#得到这个错误 enter image description here
问题
写代码的方式好吗?
如何
以求职者身份登录后,求职者可以显示仅与其技能相关的职位。如何做到这一点?
写代码的正确方式是什么?
您可以这样修改代码:
def get_queryset(self):
jobseeker = self.request.user.jobseeker # as there is OneToOne relation between user and jobseeker
# FYI please use PascalCase when defining name
jobseeker_skills = Job_Post.objects.filter(required_skill__pk__in = jobseeker.jobseeker_skill.values_list('pk', flat = True))
print(jobseeker_skills)
return jobseeker_skills
您只需在代码中将 is_jobseeker
与 jobseeker
相关联即可。
一个更干净的解决方案可以是这样的:
def get_queryset(self):
return Job_Post.objects.filter(required_skill__jobseeker__user=self.request.user)
我在 allpost
和 jobseeker
之间使用 reverse relation 以及 Job_Post
和 all_post
之间的关系来获取 Job_Post
查询集。