0

I have the following models:

class Project(models.Model): name = models.CharField(max_length=100) isprivate = models.BooleanField(default=False) class View(models.Model): name = models.CharField(max_length=100) isprivate = models.BooleanField(default=False) project = models.ForeignKey('Project', related_name='views') 

I want to get a QuerySet of all projects that are not private and have at least one View that is not private. E.g., something along the lines of:

Project.objects.filter(isprivate=False, views__isprivate=False) 

Any suggestions?

Thanks.

1 Answer 1

3

Try this:

Project.objects.filter(isprivate=False) .annotate(private_views=Count('views__isprivate')) .filter(private_views__gte=1) 

Find out more about Count() at the documentation on aggregation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. But this gives me all public projects that have one at least one private view. I want all public projects that have at least one public view. Is there a way to negate the statement in Count?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.