Replace the list API example with one from Styleguide-Example project

This commit is contained in:
Radoslav Georgiev 2020-10-25 12:14:03 +02:00
parent 6c1414c23b
commit f166c79785

View File

@ -382,21 +382,42 @@ Here are few examples: `UserCreateApi`, `UserSendResetPasswordApi`, `UserDeactiv
### An example list API ### An example list API
#### Plain
A dead-simple list API would look like that:
```python ```python
class CourseListApi(SomeAuthenticationMixin, APIView): from rest_framework.views import APIView
from rest_framework import serializers
from rest_framework.response import Response
from styleguide_example.users.selectors import user_list
from styleguide_example.users.models import BaseUser
class UserListApi(APIView):
class OutputSerializer(serializers.ModelSerializer): class OutputSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Course model = BaseUser
fields = ('id', 'name', 'start_date', 'end_date') fields = (
'id',
'email'
)
def get(self, request): def get(self, request):
courses = get_courses() users = user_list()
serializer = self.OutputSerializer(courses, many=True) data = self.OutputSerializer(users, many=True).data
return Response(serializer.data) return Response(data)
``` ```
Keep in mind this API is public by default. Authentication is up to you.
#### Filters + Pagination
You can find the code for the example list API with filters & pagination in the [Styleguide Example](https://github.com/HackSoftware/Styleguide-Example#example-list-api) project.
### An example detail API ### An example detail API
```python ```python