views:

27

answers:

1

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  A: 

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)
Daniel Roseman
Sorry, my first question description was bad. I edit the topic.
Evg