From dacd33cbafd9dd6fdd53765a9279fc614a8c4406 Mon Sep 17 00:00:00 2001 From: Cyrille Rossant Date: Tue, 24 Jan 2023 13:33:03 +0100 Subject: [PATCH] Fix failing autoname when new nickname already exists --- alyx/subjects/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/alyx/subjects/models.py b/alyx/subjects/models.py index 49ceaa46..344e8f8e 100644 --- a/alyx/subjects/models.py +++ b/alyx/subjects/models.py @@ -630,7 +630,11 @@ def new_litter_autoname(self): def new_subject_autoname(self): self.subject_autoname_index = self.subject_autoname_index + 1 self.save() - return '%s_%04d' % (self.nickname, self.subject_autoname_index) + new_name = '%s_%04d' % (self.nickname, self.subject_autoname_index) + if Subject.objects.filter(nickname=new_name).count() > 0: + return self.new_subject_autoname() + assert Subject.objects.filter(nickname=new_name).count() == 0 + return new_name def set_autoname(self, obj): if isinstance(obj, BreedingPair):