我想从多个表中过滤不同的字段。例如,用户输入是“python,web developer”。我想显示匹配作业的“python”键盘和“web开发者”标题匹配的作业。
In User table
id | username | password
-----------------------------
1 | employer1 | sffddgfd
2 | employer2 | dfggfgfd
In Company Table
id | emp_id | companyname
-----------------------------
1 | 1 | abc
2 | 2 | xyz
In jobs table
+----+--------+--------------------+
| id | emp_id | title |
+----+--------+--------------------+
| 1 | 1 | Software Developer |
| 2 | 1 | Software Developer |
| 3 | 1 | testing |
| 4 | 1 | webdeveloper |
| 5 | 2 | Software Developer |
| 6 | 2 | testing |
| 7 | 2 | software |
+----+--------+--------------------+
In employerkeyskills table
+----+--------+--------+--------------------+
| id | emp_id | job_id | keyskills |
+----+--------+--------+--------------------+
| 1 | 1 | 2 | python |
| 2 | 1 | 2 | django |
| 3 | 1 | 2 | html |
| 4 | 1 | 3 | manual testing |
| 5 | 1 | 3 | automation testing |
| 6 | 1 | 4 | css |
| 7 | 1 | 4 | javascript |
| 8 | 1 | 4 | html |
| 9 | 1 | 4 | php |
| 10 | 2 | 5 | python |
| 11 | 2 | 5 | php |
| 12 | 2 | 6 | SQL |
| 13 | 2 | 6 | Manual Testing |
| 14 | 2 | 7 | sql |
| 15 | 2 | 7 | testing |
| 16 | 2 | 7 | python |
| 17 | 2 | 7 | html |
+----+--------+--------+--------------------+
models.py
class User(models.Model):
username = models.CharField(max_length=100)
password = models.CharField(max_length=100)
class company(models.Models):
emp = models.ForeignKey(User, unique=False)
companyname = models.CharField(max_length=100)
class jobs(models.Model):
emp = models.ForeignKey(User, unique=False)
title = models.CharField(max_length=100)
class employerkeyskills(models.Model):
emp=models.ForeignKey(User,unique=False)
job=models.ForeignKey(jobs,unique=False)
keyskills=models.CharField(max_length=50)
views.py
details = employerkeyskills.objects.filter(keyskills__icontains=search)
使用上面的命令我得到了重复的结果,起初我只使用了keykills进行过滤。但同样的工作重复两次。我想过滤diplicate字段。
答案 0 :(得分:1)
您可以先分割输入字符串:
key_skill, title = "python,web developer".split(',')
然后过滤作业跨模型:
jobs = Job.objects.filter(title=title, employerkeyskills__keyskills=key_skill)
答案 1 :(得分:1)
您可以尝试:
details = employerkeyskills.objects.filter(keyskills__icontains=search).distinct()
答案 2 :(得分:0)
你可以这样做:
jobs.object.filter(employerkeyskills_set__keyskills__in = ["python", "web developer"])
如果添加related_name arg:
class employerkeyskills(models.Model):
emp=models.ForeignKey(User,unique=False)
job=models.ForeignKey(jobs,unique=False, related_name='skills')
keyskills=models.CharField(max_length=50)
jobs.object.filter(skills__keyskills__in = ["python", "web developer"])