I see in a bunch of django code, especially in viewsets, that when serializer is initialized like the following:
class UserViewSet(viewsets.ModelViewSet):
"""
A viewset that provides the standard actions
"""
queryset = User.objects.all()
serializer_class = UserSerializer
@action(detail=True, methods=['post'])
def set_password(self, request, pk=None):
user = self.get_object()
**serializer = PasswordSerializer(data=request.data)**
the data argument is always being passed in the instantiation of a Serializer instance. But I feel like I have been researching django documentation everywhere and I can't seem to find a reference to this data argument. I really want to know what exactly is data doing here, where does it come from, why is information about it so hidden?
I tried researching the official django rest framework documentation, as well as scoured several Medium articles to no avail.
In Django REST framework (DRF), the data argument you're referring to is commonly used when instantiating a serializer. This argument represents the data that you want to serialize or validate.
PasswordSerializer is a serializer class, and request.data is the input data that you want to validate and/or serialize using this serializer.
request.data: This is typically used in Django REST framework views to access the incoming data from a request. It's a dictionary-like object containing the parsed data from the request payload. In the case of a POST request, this would typically be the data sent in the request body.
When you instantiate a serializer with data=request.data, you are essentially telling the serializer to operate on this input data. The serializer will then use its defined fields and validation rules to process this data.