From 876d4e996061101bded9021962933533faf413f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Je=C5=99=C3=A1bek=20=28Jiri=20Jerabek=29?= Date: Mon, 30 Oct 2023 14:43:31 +0100 Subject: [PATCH] Add `count` to `TagSerializer` (#1621) Add count to TagSerializer fixes: #1612 --- CHANGES/1612.feature | 1 + pulp_ansible/app/serializers.py | 4 +++- pulp_ansible/app/viewsets.py | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1612.feature diff --git a/CHANGES/1612.feature b/CHANGES/1612.feature new file mode 100644 index 000000000..95e729dfb --- /dev/null +++ b/CHANGES/1612.feature @@ -0,0 +1 @@ +Display the ``count`` attribute in the tags of collections. diff --git a/pulp_ansible/app/serializers.py b/pulp_ansible/app/serializers.py index 4aa93a1a4..940f86b6b 100644 --- a/pulp_ansible/app/serializers.py +++ b/pulp_ansible/app/serializers.py @@ -385,9 +385,11 @@ class TagSerializer(serializers.ModelSerializer): A serializer for the Tag model. """ + count = serializers.IntegerField(read_only=True) + class Meta: model = Tag - fields = ["name"] + fields = ["name", "count"] class TagNestedSerializer(ModelSerializer): diff --git a/pulp_ansible/app/viewsets.py b/pulp_ansible/app/viewsets.py index af6bbe865..7a3d3913b 100644 --- a/pulp_ansible/app/viewsets.py +++ b/pulp_ansible/app/viewsets.py @@ -3,6 +3,7 @@ from django.contrib.postgres.search import SearchQuery from django.db.models import fields as db_fields from django.db.models.expressions import F, Func +from django.db.models import Count from django_filters import filters from django.http import HttpResponseRedirect, HttpResponseNotFound from drf_spectacular.utils import extend_schema @@ -1196,6 +1197,10 @@ class TagViewSet(NamedModelViewSet, mixins.ListModelMixin): queryset = Tag.objects.all() serializer_class = TagSerializer + def get_queryset(self): + qs = super().get_queryset() + return qs.annotate(count=Count("ansible_collectionversion")) + class CopyViewSet(viewsets.ViewSet): """