Есть ли способ использовать get_F00_display параллельно с values() в Django views.py

Что я хочу сделать :

Отображение человекочитаемого значения поля charfield с вариантами выбора через get_F00_display или другое в views.py, а затем в template.

models.py

class Leave(CommonFields):

LEAVES_TYPES = [
        ('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")

views.py

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

leave_list.html

<td>{{leave.type}}</td>

НО : Я хочу вернуть {{leave.get_type_display}} и в то же время Leave.objects.filter(is_active=True).**values**(...). Как это сделать? Есть ли лучший способ достичь этого?

Please don't use values. The main reason not to do this, is exactly the one you describe in the question: primitive obsession [refactoring.guru]. 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.

Вернуться на верх