How can I correct my ORM statement to show all friends not associated with a user in Django?
<p>In my Django application, I've got two models, one Users and one Friendships. There is a Many to Many relationship between the two, as Users can have many Friends, and Friends can have many other Friends that are Users.</p>

<p>How can I return all friends (first and last name) whom are NOT friends with the user with the first_name='Daniel'?</p>

<p>Models.py:</p>

<pre><code>class Friendships(models.Model):
    user = models.ForeignKey('Users', models.DO_NOTHING, related_name="usersfriend")
    friend = models.ForeignKey('Users', models.DO_NOTHING, related_name ="friendsfriend")
    created_at = models.DateTimeField(blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'friendships'


class Users(models.Model):
    first_name = models.CharField(max_length=45, blank=True, null=True)
    last_name = models.CharField(max_length=45, blank=True, null=True)
    created_at = models.DateTimeField(blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'users'
</code></pre>

<p>So far, here's what I've tried in my controller (views.py) -- please note, I understand controllers should be skinny but still learning so apologies. What I tried in the snippet below (after many failed attempts at a cleaner method) was to try and first grab friends of daniels (populating them into a list and then removing any duplicate ids), and then filter them out by their id.</p>

<pre><code># show first and last name of all friends who daniel is not friends with:

def index(req):

    friends_of_daniel = Friendships.objects.filter(user__first_name='Daniel')
    daniels_friends = []
    for friend_of_daniel in friends_of_daniel:
        daniels_friends.append(friend_of_daniel.friend.id)

    daniels_friends = list(set(daniels_friends))

    not_daniels_friends = Friendships.objects.exclude(id__in=daniels_friends)

    context = {
        'not_daniels_friends':not_daniels_friends,
    }

return render(req, "friendapp/index.html",context)
</code></pre>

<p>However, when I try the following in my views (templates) file, I still see individuals whom are friends of Daniels. Any idea what I'm doing wrong?</p>

<pre><code>&lt;ul&gt;
    {% for not_daniel_friend in not_daniels_friends %}
        &lt;li&gt;{{ not_daniel_friend.user.first_name }} {{ not_daniel_friend.user.last_name }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>I guess something like this will do. Just then take the list <code>users</code>, and get the first and last name of those users.</p>

<pre><code>daniels = Users.objects.filter(first_name="Daniel") # There may be more than one Daniel
users = Friendships.objects.exclude(friend__in=daniels)
</code></pre>

<p>Note here, while Friendships.friend is a <code>foreignkey</code> of type <code>Users</code> you can pass Users instances (i.e <code>daniels</code> list) in <code>friend__in</code> to exclude those users.</p>

python django orm many-to-many