コード例 #1
0
class ProfileViewSet(viewsets.ModelViewSet, mixins.RetrieveModelMixin):
    queryset = Profile.objects.annotate(
        following_count=aggregates.Count('user__followers', distinct=True) - 1,
        follower_count=aggregates.Count('user__followings', distinct=True) - 1,
        posts_count=aggregates.Count('user__posts', distinct=True))
    serializer_class = ProfileSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, )
    filter_backends = [filters.SearchFilter]
    search_fields = ['user__username']
    lookup_field = 'user__username'

    @decorators.action(detail=False, methods=('get', ))
    def me(self, request):
        queryset = Profile.objects.annotate(
            following_count=aggregates.Count('user__followings',
                                             distinct=True) - 1,
            follower_count=aggregates.Count('user__followers', distinct=True) -
            1,
        ).get(user=self.request.user)
        serializer = self.get_serializer(instance=queryset)
        return response.Response(serializer.data, status=status.HTTP_200_OK)

    @decorators.action(detail=True, methods=('post', ))
    def follow(self, request, user__username=None):
        profile = self.get_object()
        follow = request.user.followers.create(following=profile.user)

        return response.Response(data={'id': follow.id},
                                 status=status.HTTP_201_CREATED)

    @decorators.action(detail=True, methods=('delete', ))
    def unfollow(self, request, user__username=None):
        profile = self.get_object()
        request.user.followers.filter(following=profile.user).delete()
        return response.Response(status=status.HTTP_200_OK)
コード例 #2
0
ファイル: views.py プロジェクト: gabapppp/instagram_clone
class PostViewSet(viewsets.ReadOnlyModelViewSet, mixins.CreateModelMixin,
                  mixins.DestroyModelMixin):
    queryset = Post.objects.annotate(
        likes_count=aggregates.Count('likes', distinct=True),
        comments_count=aggregates.Count('comments', distinct=True),
    )
    serializer_class = PostSerializer
    filterset_class = PostFilterSet
    permission_classes = [
        AuthenticatedCreation,
    ]
    pagination_class = PostPagination

    def get_queryset(self):
        queryset = super().get_queryset()

        if self.action == 'list' \
                and self.request.query_params.get('username', '').strip() == '' \
                and self.request.query_params.get('feed', '').strip() == '':
            queryset = queryset.none()

        return queryset

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)
コード例 #3
0
 def me(self, request):
     queryset = Profile.objects.annotate(
         following_count=aggregates.Count('user__followings',
                                          distinct=True) - 1,
         follower_count=aggregates.Count('user__followers', distinct=True) -
         1,
     ).get(user=self.request.user)
     serializer = self.get_serializer(instance=queryset)
     return response.Response(serializer.data, status=status.HTTP_200_OK)
コード例 #4
0
ファイル: views.py プロジェクト: ContextSwitchWang/db-project
 def all_inventory_count(self):
     return models.Item.objects.values_list('inventory__name').annotate(
             agg.Count('id'))
コード例 #5
0
 def queryset(self, request, queryset):
     queryset = queryset.annotate(local_copies_count=aggregates.Count('local_copies'))
     if self.value() == '1':
         return queryset.exclude(local_copies_count=0)
     if self.value() == '0':
         return queryset.filter(local_copies_count=0)
コード例 #6
0
 def annotate(self):
     # TMP: meh, we could model count as an aggregation
     #     (caveat: count is technically *not* aggregating a field here,
     #     whereas our aggregation operators do)
     return aggregates.Count('pk')  # Is there a way to count(*) ?