Jasmine Rain
Jasmine Rain

Reputation: 449

Django filter all that has certain names

I have a problem very similar to Django filter queryset __in for *every* item in list

I need to select all prospects that have certain certifications as defined by

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)

For example, if I have certifications ['CPA', 'PhD'] I want all prospects that have exactly CPA & PhD, if they have more or less than those two, I want to exclude them.

I'm using this solution, as give by the link earlier

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

    return prospects

But I'm receiving an error of ValueError: invalid literal for int() with base 10: 'CPA'

Upvotes: 0

Views: 86

Answers (1)

user8060120
user8060120

Reputation:

Try it:

prospects.filter(certification__name__in=certifications)

Upvotes: 1

Related Questions