1

I'm just a newbie in python and django, and I'm quite lost on my current issue, this problem somehow not new to some, hoping to get some guidance for me to solve this matter.

I performed my first query to get a list of foreign key values and used a loop to pass the individual value and used it as a param to my second query. Technically I did my second query inside the iteration of the loop. Then I used and get the iterated values of my second query and pass it on the self.paginate_queryset in order for me to serialize it and produce the response on my get_paginated_response.

As part of observing, I printed all the result of my second query inside the said loop earlier, and it worked properly, but when I tried to run it on postman, I noticed the response only produced one result from my series of results base on the given loop.

What exactly do I need to do to produce all the needed results?

Feel free also to put any suggestions if you notice something on my codes that need improvements.

Here is my function in views.py:

def get(self, request, *args, **kwargs):
    global friend, request_queries
    self.pagination_class = PageNumberPagination

    friends = Friend.objects.filter(from_user=request.user.pk)
    for friend in friends:
        request_queries = GosamFriendProfile.objects.raw("""SELECT *
                            FROM gosam_friend_profile
                            INNER JOIN friendship_friend ON gosam_friend_profile.gosam_friends_id = friendship_friend.id
                            INNER JOIN profile ON gosam_friend_profile.profile_id = profile.id
                            WHERE profile.id = %s
                            ORDER BY profile.first_name""" % friend.to_user_id)

    query_set = self.paginate_queryset(request_queries)
    serializer = GosamFriendProfileSerializer(query_set, many=True)

    return self.get_paginated_response(serializer.data)

serializer:

 class GosamFriendProfileSerializer(serializers.ModelSerializer):

    class Meta:

        model = GosamFriendProfile
           fields = '__all__'
           depth = 1

GosamFriendProfile model

  # Junction Table for Gosam Friends and Profile
  class GosamFriendProfile(models.Model):
     gosam_friends = models.ForeignKey(Friend, 
       on_delete=models.CASCADE, null=False)
     profile = models.ForeignKey(Profile, on_delete=models.CASCADE, 
       null=False)

     class Meta:
       db_table = 'gosam_friend_profile'
2
  • 1
    I know your new but you should really try to avoid using raw SQL queries, you almost never need to use one and if you ever do, it's likely bad database design. You really shouldn't need to use any globals either. Commented Apr 3, 2020 at 7:30
  • Yep, noted. Thanks for the comment. :) Commented Apr 3, 2020 at 7:37

1 Answer 1

1

I've made a few suggestions in the comments but the answer to your question is that you are looping through friends and assigning a new variable inside the loop called request_queries. Every single time it loops, your are reassigning the variable. So when you do query_set = self.paginate_queryset(request_queries) you are only passing the result of your last iteration of the for loop.

Just a note to change this:

    for friend in friends:
        request_queries = GosamFriendProfile.objects.raw("""SELECT *
                            FROM gosam_friend_profile
                            INNER JOIN friendship_friend ON gosam_friend_profile.gosam_friends_id = friendship_friend.id
                            INNER JOIN profile ON gosam_friend_profile.profile_id = profile.id
                            WHERE profile.id = %s
                            ORDER BY profile.first_name""" % friend.to_user_id)

To this without the for loop.

request_queries = GosamFriendProfile.objects.filter(
    gosam_friends__in=friends,
).select_related('profile', 'friend').order_by('profile__first_name')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I noticed it earlier also. Can I ask what possible solution/s for me to implement in order to avoid this problem and get all the results and pass it on my response?
I've updated my answer to show you the different queries. With the change you don't need a for loop which means you are not hitting the database for every iteration. Have a read through: docs.djangoproject.com/en/3.0/topics/db/queries. The .select_related() does the INNER JOIN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.