Skip to content

Commit

Permalink
Add count to TagSerializer (#1621)
Browse files Browse the repository at this point in the history
Add count to TagSerializer

fixes: #1612
  • Loading branch information
jerabekjiri authored Oct 30, 2023
1 parent 7191d48 commit 876d4e9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/1612.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Display the ``count`` attribute in the tags of collections.
4 changes: 3 additions & 1 deletion pulp_ansible/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions pulp_ansible/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 876d4e9

Please sign in to comment.