From a5e802f3700e7c3a49c652774f6c4fb3ad997542 Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Mon, 18 Sep 2023 17:12:31 +0200 Subject: [PATCH] Improved the error message when trying to duplicate a Trust --- apps/note/api/serializers.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/note/api/serializers.py b/apps/note/api/serializers.py index 33bf75ba..2709b4a8 100644 --- a/apps/note/api/serializers.py +++ b/apps/note/api/serializers.py @@ -11,6 +11,7 @@ from member.models import Membership from note_kfet.middlewares import get_current_request from permission.backends import PermissionBackend from rest_framework.utils import model_meta +from rest_framework.validators import UniqueTogetherValidator from ..models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias, Trust from ..models.transactions import TransactionTemplate, Transaction, MembershipTransaction, TemplateCategory, \ @@ -86,11 +87,9 @@ class TrustSerializer(serializers.ModelSerializer): class Meta: model = Trust fields = '__all__' - - def validate(self, attrs): - instance = Trust(**attrs) - instance.clean() - return attrs + validators = [UniqueTogetherValidator( + queryset=Trust.objects.all(), fields=('trusting', 'trusted'), + message=_("You already have that person as a friend"))] class AliasSerializer(serializers.ModelSerializer):