0

I have models:

class Site(models.Model):
    profile = models.ForeignKey(User)

class Profile(models.Model):
    blacklist = models.ManyToManyField(Site)

How can i do equivalent of this query via django orm?

SELECT * FROM site WHERE 2 NOT IN (SELECT site_id FROM profile_blacklist WHERE profile_site.profile_id=site.profile_id)

I need some kind of blacklist filter. Each site has user (profile). This user has black list of sites. I do search for site with id=2 for exapmle. And i need sites witch owners has no site number 2 in blacklist.

1 Answer 1

1

Don't think in terms of SQL. Think in terms of what result you want.

I don't really understand why you're using a subquery in that SQL anyway. It seems that what you want to do is find all the sites that have a category whose ID is 2. Is that right?

If so, what you want is:

Site.objects.filter(category__id=2)
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, my first question description was bad. I edit the topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.