[Django]-Django – How do I get display name inside queryset .values()

5👍

You can get to related object fields by using __.

qs = self.model.objects.filter(
    date__range=(start_date, end_date)
).values(
    "customer__name"
)
👤Anna

1👍

You can also use __ within your values like this .values("customer__name") to select field on related table.

0👍

Use this code to get all related customer names.

class Totals(Manager):

def by_customer_and_date(self, start_date, end_date):

    name_list = self.model.objects.filter(
        date__range=(start_date, end_date)
    ).values("customer__name", flat=True)

    return name_list
👤Rajesh

Leave a comment