Is there a way to use get_F00_display in parallel with values() in Django

What I want to do :

Display the human readable value of a charfield with choices via get_F00_display or other in and then in template.

class Leave(CommonFields):

        ('10', _('Type 1')),
        ('20', _('Type 2')),
        ('30', _('Type 3')),

owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
type = models.CharField(max_length=3, choices=LEAVES_TYPES, null=True, default="10")

def __str__(self):
    return self.owner.first_name + " " + self.owner.last_name + " : du " + self.begin_date.strftime("%d-%m-%Y") + " au " + self.end_date.strftime("%d-%m-%Y")

def get_queryset(self):
    return Leave.objects.filter(is_active=True).values('type', 'begin_date','end_date','range','comment','status')



BUT : I want to return {{leave.get_type_display}} and at the same time Leave.objects.filter(is_active=True).**values**(...) How to ? Is there a better way to achieve this ?

Please don't use values. The main reason not to do this, is exactly the one you describe in the question: primitive obsession []. If you want to limit the number of columns that you aim to fetch, you can use .only(…) [Django-doc] or .defer(…) [Django-doc] to specify what columns not to fetch:

def get_queryset(self):
    return Leave.objects.filter(is_active=True).only(
        'type', 'begin_date', 'end_date', 'range', 'comment', 'status'

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Back to Top