Filter- 模型的查询集基于来自另一个模型的 ManyToMany 字段的匹配
Filter- model's query set based on the match from another model's ManyToMany fields
我有这个问题,我想列出申请人仅能够的项目;我有这样的模型>>
class Project(models.Model):
project_title= models.CharField(max_length=100)
categories = models.ManyToManyField('ProjectCategory')
class Applicant(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
full_name = models.CharField(max_length=50, null=True, blank=True)
categories = models.ManyToManyField('ProjectCategory')
class ProjectCategory(models.Model):
category = models.CharField(max_length=50)
申请人有类别字段来指定he/she的能力;假设选择“python”和“Django.
还创建了两个项目,其中>>
第一个项目:(project_title:需要 Django 开发人员)[类别字段:需要“python”和'Django']
第二个项目:project_title:Need 开发人员[类别字段:需要“python”&'Django'&'Django-Rest']
我怎样才能制作查询集,我只能显示申请人只有能力或感兴趣的第一个项目?
假设我们将存储用户在 Applicant
模型中选择的 categories
,
# Getting the categories for the applicant
categories_for_applicant = Applicant.objects.filter(user=user_obj
).first().categories.values_list(
"category", flat=True)
# Getting the list of projects based on categories for applicant
projects_list = Project.objects.filter(
categories__category__in=categories_for_applicant)
# To filter dynamically for a list of categories, assuming below for example
categories = ["python", "django"]
# Since django uses lazy query, this doesn't make a DB call
project_objects = Project.objects.all()
for category in categories:
# This will give all projects containing lets say "python"
# Now we have objects with python as categories
# In second iteration we will filter again on those with "django". Now we have projects with both python and django
project_objects = project_objects.filter(categories__name=category)
# Final project objects will contain projects objects with both python and django and so on.
我有这个问题,我想列出申请人仅能够的项目;我有这样的模型>>
class Project(models.Model):
project_title= models.CharField(max_length=100)
categories = models.ManyToManyField('ProjectCategory')
class Applicant(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
full_name = models.CharField(max_length=50, null=True, blank=True)
categories = models.ManyToManyField('ProjectCategory')
class ProjectCategory(models.Model):
category = models.CharField(max_length=50)
申请人有类别字段来指定he/she的能力;假设选择“python”和“Django. 还创建了两个项目,其中>>
第一个项目:(project_title:需要 Django 开发人员)[类别字段:需要“python”和'Django']
第二个项目:project_title:Need 开发人员[类别字段:需要“python”&'Django'&'Django-Rest']
我怎样才能制作查询集,我只能显示申请人只有能力或感兴趣的第一个项目?
假设我们将存储用户在 Applicant
模型中选择的 categories
,
# Getting the categories for the applicant
categories_for_applicant = Applicant.objects.filter(user=user_obj
).first().categories.values_list(
"category", flat=True)
# Getting the list of projects based on categories for applicant
projects_list = Project.objects.filter(
categories__category__in=categories_for_applicant)
# To filter dynamically for a list of categories, assuming below for example
categories = ["python", "django"]
# Since django uses lazy query, this doesn't make a DB call
project_objects = Project.objects.all()
for category in categories:
# This will give all projects containing lets say "python"
# Now we have objects with python as categories
# In second iteration we will filter again on those with "django". Now we have projects with both python and django
project_objects = project_objects.filter(categories__name=category)
# Final project objects will contain projects objects with both python and django and so on.