User Feed Including Following:
tweet_feed_view:
@api_view([‘GET’]) # http method the client === GET @permission_classes([IsAuthenticated]) def tweet_feed_view(request, args, *kwargs): user = request.user profiles = user.following.all() # user.following.all() -> All user profiles this user follow followed_users_id = [] if profiles.exists(): followed_users_id = [x.user.id for x in profiles] followed_users_id.append(user.id) qs = Tweet.objects.filter(useridin=followed_users_id).order_by(“-timestamp”) serializer = TweetSerializer(qs, many=True) return Response(serializer.data, status=200)
…
- import tweet_feed_view in /tweets/api/urls.py```pythonfrom django.contrib import adminfrom django.urls import pathfrom .views import (tweet_detail_view,tweet_list_view,tweet_create_view,tweet_delete_view,tweet_action_view,tweet_feed_view, # new)urlpatterns = [path('', tweet_list_view),path('feed/', tweet_list_view), # newpath('action/', tweet_action_view),path('create/', tweet_create_view),path('<int:tweet_id>/', tweet_detail_view),path('<int:tweet_id>/delete/', tweet_delete_view),]
to test, runserver and access http://localhost/api/tweets/feed/
there is no actually following relationship found, and we need a more efficient way to call profiles in the database.
More Efficient Backend Lookups and Custom Model Managers:
from django.db.models import Q
class TweetQuerySet(models.QuerySet): def by_username(self, username): return self.filter(userusernameiexact=username)
def feed(self, user):profiles_exist = user.following.exists()followed_users_id = []if profiles_exist:followed_users_id = user.following.values_list("user__id", flat=True) # [x.user.id for x in profiles]return self.filter(Q(user__id__in=followed_users_id) |Q(user=user)).distinct().order_by("-timestamp")# use query to pass user id and user# distint for the same element only appears once# order by reversed time
class TweetManager(models.Manager): def get_queryset(self, args, *kwargs): return TweetQuerySet(self.model, using=self._db)
def feed(self, user):return self.get_queryset().feed(user)
…
- and use those new models and model methods in /tweets/api/views.py```python@api_view(['GET'])@permission_classes([IsAuthenticated])def tweet_feed_view(request, *args, **kwargs):user = request.user# profiles = user.following.all() # user.following.all() -> All user profiles this user follow# if profiles.exists():# followed_users_id = [x.user.id for x in profiles]# followed_users_id.append(user.id)# qs = Tweet.objects.filter(user__id__in=followed_users_id).order_by("-timestamp")qs = Tweet.objects.feed(user)serializer = TweetSerializer(qs, many=True)return Response(serializer.data, status=200)@api_view(['GET'])def tweet_list_view(request, *args, **kwargs):qs = Tweet.objects.all()username = request.GET.get('username')if username != None:# qs = qs.filter(user__username__iexact=username)qs = qs.by_username(username) # moved this part to a method in TweetQuerySet()serializer = TweetSerializer(qs, many=True)# return Response(serializer.data)return Response( serializer.data,status=200)
