Writing UserSerializer and UserViewset
Explore how to write a UserSerializer to convert Django model data into JSON format and validate fields such as unique emails. Learn to build a UserViewset to manage user data with GET and PATCH methods, integrating Django REST Framework for effective authentication and authorization using JWTs.
We'll cover the following...
A serializer allows us to convert complex Django complex data structures such as QuerySet or model instances into Python native objects that can be easily converted to JSON or XML format. However, a serializer also serializes JSON or XML to native Python. Django Rest Framework (DRF) provides a serializers package we can use to write serializers and validations when API calls are made to an endpoint using this serializer.
Note: Please look at the appendix to see how to install the
djangorestframeworkpackage. We have also addeddjango-filterfor data filtering support.
Let’s add rest_framework to the ...