Reputation: 7119
I want to make a model with user ID as one of fields, but I want it to be created without needing to fill in userID field in POST request. Instead, I want the userID to be auto-filled from token authentication. How is this done?
Upvotes: 1
Views: 48
Reputation: 11429
You should have a look at the rest framework tutorial. You can pass the user_id
or user
instance to the save method of your serializer. This can be done in the perform_create
method of your ViewSet
class SnippetViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Upvotes: 2