From edda2beded1d653725045df7fc62bf806b2168bd Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Fri, 11 Nov 2016 08:26:03 -0500 Subject: [PATCH] apply optimization to avoid query for count of JT labels --- awx/api/serializers.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 986f5c8c1e..d14bdd97c8 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1792,7 +1792,12 @@ class OrganizationCredentialSerializerCreate(CredentialSerializerCreate): class LabelsListMixin(object): def _summary_field_labels(self, obj): - return {'count': obj.labels.count(), 'results': [{'id': x.id, 'name': x.name} for x in obj.labels.all().order_by('name')[:10]]} + label_list = [{'id': x.id, 'name': x.name} for x in obj.labels.all().order_by('name')[:10]] + if len(label_list) < 10: + label_ct = len(label_list) + else: + label_ct = obj.labels.count() + return {'count': label_ct, 'results': label_list} def get_summary_fields(self, obj): res = super(LabelsListMixin, self).get_summary_fields(obj)