Django过滤所有具有特定名称的

时间:2017-07-25 20:28:10

标签: django django-queryset

我有一个非常类似的问题 Django filter queryset __in for *every* item in list

我需要选择

定义的具有某些prospects的所有certifications
class Prospect(models.Model):
    certification = models.ManyToManyField(Certification, blank=True, related_name="certification_prospects")

class Certification(models.Model):
    name = models.CharField(max_length=200, null=True, blank=True)

例如,如果我有认证['CPA','PhD'],我希望所有具有CPA和CPA的潜在客户博士,如果他们有多于或少于这两个,我想排除他们。

我正在使用这个解决方案,正如之前的链接所给出的那样

def searchForProspectByCertifications(prospects, certifications):
    prospects = prospects.filter(certification__in=certifications).annotate(num_certifications=Count('certification')).filter(num_certifications=len(certifications))

    return prospects

但我收到错误ValueError: invalid literal for int() with base 10: 'CPA'

1 个答案:

答案 0 :(得分:1)

试一试:

prospects.filter(certification__name__in=certifications)