Created
August 21, 2023 13:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetLeaderSerializer(serializers.Serializer): | |
user = serializers.PrimaryKeyRelatedField(queryset=CustomUser.objects.all()) | |
leader = serializers.PrimaryKeyRelatedField(queryset=CustomUser.objects.all()) | |
def validate(self, data): | |
""" | |
Check that a user can't be a leader of himself. | |
Check that a circular relationship is not allowed. | |
""" | |
data = super().validate(data) | |
user = data["user"] | |
leader = data["leader"] | |
if user.id == leader.id: | |
raise ValidationError("The user can't be the same as the leader.") | |
if leader.leader_id == user.id: | |
raise ValidationError("Circular relationship not allowed.") | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment