Best approach when implementing Serializers #9428
Unanswered
PedroDaumas
asked this question in
Question & Answer
Replies: 1 comment
-
Personally, I would prefer to write a serializer for every action and override get_serializer_class, or at least separate them (read/write&update). |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone, I've recently started using DRF to build an API and it's been great, but I'm not sure how I should go about implementing Serializers for CRUD operations. What is the best approach, have multiple Serializers one for each operation/endpoint? Have a general one with extra logic to remove/add fields depending on the operation?
Although self-explanatory let me show an example of my problem, let's say I have a default user, whenever I'm creating a Sign-Up route there are a few fields that I don't want the person requesting to be able to send, ie: is_superuser or is_staff.
So I create something like this:
This should be enough for a Sign-Up route, but now let's say I want to create an Update route. Since username is a unique field I don't want the user to be able to send this field in either PATCH or PUT request, so I need to exclude these fields for said methods. I might also want to have a user send the current password before changing it, and so on.
Beta Was this translation helpful? Give feedback.
All reactions