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, 2020 in Java-Script by kartik
• 37,520 points
10,921 views

1 answer to this question.

+1 vote

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, 2020 by Niroj
• 82,840 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, 2020 in Java-Script by Niroj
• 82,840 points
863 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, 2020 in Java-Script by Niroj
• 82,840 points
19,519 views
0 votes
0 answers

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

How to access PHP session variables from ...READ MORE

Jul 7, 2020 in Java-Script by kartik
• 37,520 points
1,221 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, 2020 in Java-Script by Niroj
• 82,840 points
3,321 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, 2020 in Laravel by Niroj
• 82,840 points
1,682 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, 2020 in Laravel by Niroj
• 82,840 points
1,442 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, 2020 in Laravel by Niroj
• 82,840 points
1,220 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, 2020 in Laravel by Niroj
• 82,840 points
1,017 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, 2020 in Java-Script by Niroj
• 82,840 points
18,621 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, 2020 in Java-Script by Niroj
• 82,840 points
12,923 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP