Search code examples
djangodjango-modelsdjango-orm

The annotation 'id' conflicts with a field on the model


In Annotate I am trying to get the count of quires for which is_healthy is True but I am getting an Error The annotation 'id' conflicts with a field on the model. Any solution to solve this? and why is this causing how can i resolve this?

DeviceHealthHistory.objects.filter(**filter_data).values(
  id = F('id'),
).annotate(
   healthy_count=Count('id', filter=Q(is_healthy=True)),
)

Solution

  • If you are just looking for count then you use count function fo queryset:

    DeviceHealthHistory.objects.filter(**filter_data).filter(is_healthy=True).count()
    

    To get other fields along with the count.

    DeviceHealthHistory.objects.filter(**filter_data).values(
      'other_field1'
    ).annotate(
       healthy_count=Count('id', filter=Q(is_healthy=True)),
    )