How to Get values from a specific user - queryset in Django?

0 votes

I stuck with this queryset, no idea how to write it properly. So I'd like to see all related users to this specific user, in other words I have a friend list.

models.py

class Friend(models.Model):
    user = models.ForeignKey(User, related_name="friendship_creator_set", on_delete=models.CASCADE)
    friend = models.ForeignKey(User, related_name="friend_set", on_delete=models.CASCADE)

views.py

def profile(request):
    friends = Friend...

    context = {
        'friends': friends,
    }

    return render(request, ..., context)

How to write queryset to get list of usernames who are firends to specific user?

May 28 in Java-Script by kartik
• 30,630 points
526 views

1 answer to this question.

0 votes

Hello @kartik,

You can get the list of Users that are friends with a given user through:

friends = User.objects.filter(friendship_creator_set__friend=request.user)

or if you want to obtain the Users that request.user considers friends (so the relation in reverse), you can use:

friends = User.objects.filter(friend_set__user=request.user)

These are the User objects. We can also obtain the Friend objects, with:

friends = Friend.objects.filter(friend=request.user)

or we can obtain the user names with:

usernames = User.objects.filter(
    friendship_creator_set__friend=request.user
).value_list('username', flat=True)

Hope this works!!

answered May 28 by Niroj
• 66,420 points

Related Questions In Java-Script

0 votes
1 answer

How to customize the auth.User Admin page in Django CRUD?

Hello @kartik, Try this in admin.py file : from ...READ MORE

answered Jun 12 in Java-Script by Niroj
• 66,420 points
101 views
0 votes
1 answer

How to send data in request body with a GET when using jQuery $.ajax()?

Hello @kartik, Sending the data in your scenario,I ...READ MORE

answered Jun 18 in Java-Script by Niroj
• 66,420 points
2,524 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello @kartik, You can produce the javascript file ...READ MORE

answered Jul 7 in Java-Script by Niroj
• 66,420 points
82 views
0 votes
1 answer

How to check if a specific key is present in a hash or not?

Hello @kartik, While Hash#has_key? gets the job done, as it has ...READ MORE

answered Jul 24 in Java-Script by Niroj
• 66,420 points
126 views
0 votes
1 answer

What is Laravel framework? Why one should use Laravel?

Laravel is a PHP web-framework; it utilized ...READ MORE

answered Mar 17 in Laravel by Niroj
• 66,420 points
200 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17 in Laravel by Niroj
• 66,420 points
196 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17 in Laravel by Niroj
• 66,420 points
82 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17 in Laravel by Niroj
• 66,420 points
88 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28 in Java-Script by Niroj
• 66,420 points
638 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29 in Java-Script by Niroj
• 66,420 points
2,590 views