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'