From ae0d1a080e693223ae80e14e001fdbe37ca95873 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 26 Oct 2021 15:07:37 +0200 Subject: [PATCH] Fix borrowable autocompletion --- media/models.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/media/models.py b/media/models.py index 669ee7a..e16c65d 100644 --- a/media/models.py +++ b/media/models.py @@ -44,7 +44,17 @@ class Borrowable(PolymorphicModel): ) def __str__(self): - return self.title + obj = self + if obj.__class__ == Borrowable: + # Get true object instance, useful for autocompletion + obj = Borrowable.objects.get(pk=obj.pk) + + title = obj.title + if hasattr(obj, 'subtitle'): + subtitle = obj.subtitle + if subtitle: + title = f"{title} : {subtitle}" + return title class Meta: verbose_name = _('borrowable') @@ -99,12 +109,6 @@ class Book(Medium): null=True, ) - def __str__(self): - if self.subtitle: - return "{} : {}".format(self.title, self.subtitle) - else: - return self.title - class Meta: verbose_name = _("book") verbose_name_plural = _("books")