Hello everyone,
I am trying to do something like a group by in SQL but with the Django ORM.
In addition, I am using Django Rest Framework and I would like the result of the group by be sent to a serializer to create an API.
Here are my models :
class Host(models.Model):
hostname = models.CharField(max_length=255)
operating_system = models.CharField(max_length=255, null=True, blank=True)
class Software(models.Model):
name = models.CharField(max_length=255)
version = models.CharField(max_length=255, null=True, blank=True)
host = models.ForeignKey(Host, on_delete=models.CASCADE)
Here are my serializers :
class HostSerializer(serializers.ModelSerializer):
class Meta:
model = Host
exclude = ('id',)
class SoftwareSerializer(serializers.ModelSerializer):
host = HostSerializer()
class Meta:
model = Software
exclude = ('id',)
I would like to retrieve the distinct software (with the version of each one), and the hosts that have these software installed.
I saw somewhere that I may use aggregation. But the aggregation return a dict, not an object.