Skip to content

Instantly share code, notes, and snippets.

@brunomichetti
Created August 21, 2023 13:12
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