I django, we can write validators on serializer or model level. What is the difference between these two options? Which of them to choose in different situations?
You want a model level validation when this validation applies to all cases in your model.
Let’s say you have a
User and you always want to validate that the username is lowercased, then you validate this on a Model level.
But in the other hand, if you want specific validation, you do it in the serializer. In the same
User example, if you wanted to validate that the username is
lowercased only on a register scenario, you can create a RegisterSerializer and do the validation there.