'Do passing Django Queryset to method triggers database

I'm using Django for my backend and I'm wondering whether doing following thing hits the database.

I know Django Querysets are lazy thus doing this only hits the database once :

records = RecordModel.objects.all()
records = records.filter(id__in=[1, 2, 3])
records = records.filter(name__in=["some", "random", "strings"])
print(records)

In that case the database is really triggered when we try to print records.

But when passing Queryset as method parameter. Is the database hit 2 times ?

For instance:

def some_api_endpoint(request):
    records = RecordModel.objects.all()
    records = RecordModel.filter_queryset(records, request.query_params)
    return Response(MySerializer(records, many=True).data)

where filter_queryset performs some filtering on the queryset according to the request parameters, like this :

@staticmethod
def filter_queryset(queryset, query_params):
    name = query_params.get("name")
    if name:
        queryset = queryset.filter(name__icontains=name)
    # Other potential filters here...
    return queryset

I'm wondering here if records are evaluated when passing it to filter_queryset AND when returning them in the Response ?

Thanks in advance :) !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source