DRF – How to get a single item from a QuerySet?

I want to get objects with the pk I send through request but I want only one item from the queryset.

I want BatchLog objects that their batch_id is same as the pk and my query returns multiple items within that query. I just want one of them and it doesn’t matter which one it is.

def get_queryset(self):
    return BatchLog.objects.filter(batch_id=self.kwargs["pk"])

It returns QuerySet<[BatchLog, BatchLog]> but I need QuerySet<BatchLog>

How can I achieve it?



You can just get the first one.

def get_queryset(self):
    return BatchLog.objects.filter(batch_id=self.kwargs["pk"]).first()

def get_object(self):
    queryset = self.get_queryset()
    return get_object_or_404(queryset)