0

I would like to ask how I can drop data from the QuerySet when the value of the attribute is null.

Models.py:

class RnDProject(models.Model):
    name = models.CharField(max_length=100)
class Yhrtaskgrp(models.Model):
    employee_name = models.CharField(max_length=100)
    employee_id = models.CharField(max_length=10)
    hours_worked = models.FloatField()
    assigned_RnD = models.ForeignKey(RnDProject, on_delete=models.SET_NULL)

views.py:

class RndView(APIView):
    def get(self, request):
        qs = Yhrtaskgrp.objects.values('employee_name', 'employee_id').annotate(rnd_hours=Sum('hours_worked', filter=Q(assigned_RnD_id__isnull=False)), all_hours=Sum('hours_worked'))
    return Response(qs)

Object from the qs can look like this when the ForeingKey is null:

    {
        "employee_id": "123456",
        "employee_name": "John Doe",
        "rnd_hours": null,
        "all_hours": 20
    }

I would like to drop all objects with rnd_hours == null from the QuerySet. Is there anyway how django can drop these objects?

pipikej
  • 285
  • 3
  • 17

1 Answers1

0

As Vishal Singh mentioned in the comment I had to add .exclude(rnd_hours__isnull=True) to the qs.

pipikej
  • 285
  • 3
  • 17