Allow overriding UidAndTokenSerializer's behaviour #692
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
UidAndTokenSerializer
validates both fieldsuid
andtoken
at once and there's no way to reuse its code, because it doesn't allow overriding the validation of a single field without the other (eitheruid
ANDtoken
or neither).This change splits the validation into the respective fields to allow reusage. In my case, I needed to reuse
UidAndTokenSerializer
with a differenttoken
logic but with the sameuid
logic.c74d590
@konradhalas left a comment 3 years ago that I do not quite understand:
# uid validation have to be here, because validate_<field_name>
# doesn't work with modelserializer
But
UidAndTokenSerializer
does not inherit frommodelserializer
!For me the field validation works fine. It's also the same thing in the example provided by
rest_framework
, see: https://www.django-rest-framework.org/api-guide/serializers/#field-level-validationI also left a comment at
validate_token
regarding theself.user
. The reason I did not use any of the alternatives I mentioned is that if it broke in the future, then rest_framework must have had major changes that shouldn't go unspotted in that specific method.Also, is it possible to update the pip repository? https://pypi.org/project/djoser/
Released: Oct 30, 2020
Plenty of updates have took place since then.
If this doesn't get accepted and someone is looking for a workaround, to "reuse"
UidAndTokenSerializer
, you just need to provide your owndef validate_token
anddef validate_uid
and you'd have to copy the code of one of them (the one you don't want to override) from UidAndTokenSerializer for one field and provide your own code for the second one (you one you want to override).You also need to override 'def validate' to do nothing:
class My_UidAndTokenSerializer(UidAndTokenSerializer):
At this point, it's not even called 'reuse', so... 🍡