Sumason
Sumason

Reputation: 77

Get everything that a Many-To-Many Field does not point to

Right now my Model looks a little something like this:

PageView:
    ...
    userList = models.ManytoManyField("core.UserProfile")

UserProfile:
    ...
    pageView = models.OnetoOneField(PageView)

The idea is that a UserProfile has a reference to a PageView that they own. PageView references many profiles, and those profiles can see who follows them.

What I would like is a way to grab all of the profiles that are not referenced by a particular PageView.

For example:

If we have Users Chris, Bob and Fred and Chris is only following Bob. Getting chris.pageView.userList has only Bob. I would like a list that includes all of the other profiles excluding Bob (IE. Chris[The current user] and Fred).

I've tried to be through in explaining what I am trying to do, and I'm still fairly new to Django, so it's quite possible that I'm doing things horribly wrong. Please leave comments to A) help me improve my question or B) fix my models so that its easier to retrieve this type of infromation.

Upvotes: 1

Views: 60

Answers (1)

Ismail Badawi
Ismail Badawi

Reputation: 37187

UserProfile.objects.exclude(id__in=chris.pageView.userList.all())

Upvotes: 2

Related Questions