Change ModelSerializer to Serializer in examples

This commit is contained in:
Radoslav Georgiev 2021-11-16 17:09:44 +02:00
parent ea5ee0f047
commit f200a06334
No known key found for this signature in database
GPG Key ID: 0B7753A4DFCE646D

View File

@ -623,13 +623,9 @@ from styleguide_example.users.models import BaseUser
class UserListApi(APIView): class UserListApi(APIView):
class OutputSerializer(serializers.ModelSerializer): class OutputSerializer(serializers.Serializer):
class Meta: id = serializers.CharField()
model = BaseUser email = serializers.CharField()
fields = (
'id',
'email'
)
def get(self, request): def get(self, request):
users = user_list() users = user_list()
@ -677,14 +673,10 @@ class UserListApi(ApiErrorsMixin, APIView):
is_admin = serializers.NullBooleanField(required=False) is_admin = serializers.NullBooleanField(required=False)
email = serializers.EmailField(required=False) email = serializers.EmailField(required=False)
class OutputSerializer(serializers.ModelSerializer): class OutputSerializer(serializers.Serializer):
class Meta: id = serializers.CharField()
model = BaseUser email = serializers.CharField()
fields = ( is_admin = serializers.BooleanField()
'id',
'email',
'is_admin'
)
def get(self, request): def get(self, request):
# Make sure the filters are valid, if passed # Make sure the filters are valid, if passed
@ -806,10 +798,11 @@ You can find the code for the example list API with filters & pagination in the
```python ```python
class CourseDetailApi(SomeAuthenticationMixin, APIView): class CourseDetailApi(SomeAuthenticationMixin, APIView):
class OutputSerializer(serializers.ModelSerializer): class OutputSerializer(serializers.Serializer):
class Meta: id = serializers.CharField()
model = Course name = serializers.CharField()
fields = ('id', 'name', 'start_date', 'end_date') start_date = serializers.DateField()
end_date = serializers.DateField()
def get(self, request, course_id): def get(self, request, course_id):
course = get_course(id=course_id) course = get_course(id=course_id)
@ -869,6 +862,10 @@ class Serializer(serializers.Serializer):
The implementation of `inline_serializer` can be found [here](https://github.com/HackSoftware/Styleguide-Example/blob/master/styleguide_example/common/utils.py#L34), in the [Styleguide-Example](https://github.com/HackSoftware/Styleguide-Example) repo. The implementation of `inline_serializer` can be found [here](https://github.com/HackSoftware/Styleguide-Example/blob/master/styleguide_example/common/utils.py#L34), in the [Styleguide-Example](https://github.com/HackSoftware/Styleguide-Example) repo.
### Advanced serialization
*Coming soon*
## Urls ## Urls
We usually organize our urls the same way we organize our APIs - 1 url per API, meaning 1 url per action. We usually organize our urls the same way we organize our APIs - 1 url per API, meaning 1 url per action.