I have url api/v1/posts/. If we use GET request from that url, we can see all objects. For example:
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://127.0.0.1:8000/api/v1/posts/11/",
"id": 12,
"name": "Sample name",
"type": "Shirt",
"collection": "Winter 2020"
}
]
}
We need to create functional -
We can create GET request with filter. In our case, we need to filter "type": "Shirt" and we can see "Collection" with count (count shirts from all collections)
For example:
{
"collection" : "Winter 2020"
"result" : [
{
"count": 12
}
]
}
Etc... How to create it using Django Rest Framework?
UPD (Added view):
class PostViewSet(LikedMixin, viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
authentication_classes = (TokenAuthentication, SessionAuthentication, )
filter_backends = (DjangoFilterBackend, )
filterset_fields = ('name', 'type')
def perform_create(self, serializer):
serializer.save(author=self.request.user)