Search code examples
pythondjangoormmany-to-many

How can I correct my ORM statement to show all friends not associated with a user in Django?


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.

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

Models.py:

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'

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.

# 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)

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?

<ul>
    {% for not_daniel_friend in not_daniels_friends %}
        <li>{{ not_daniel_friend.user.first_name }} {{ not_daniel_friend.user.last_name }}</li>
    {% endfor %}
</ul>

Solution

  • Try this,In the place of friend_of_daniel.friend.id , You should exclude the results from User model.

    Something like this :

        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 = Users.objects.exclude(id__in=daniels_friends)
    
            context = {
                'not_daniels_friends':not_daniels_friends,
            }
    
        return render(req, "friendapp/index.html",context)
    

    Thanks.