update values in loop
This commit is contained in:
parent
664f081b9f
commit
ecc886689e
|
@ -21,6 +21,12 @@ class TmcElementSerializer(serializers.Serializer):
|
||||||
count = serializers.IntegerField()
|
count = serializers.IntegerField()
|
||||||
|
|
||||||
|
|
||||||
|
class TerritorySerializer(serializers.Serializer):
|
||||||
|
id = serializers.IntegerField()
|
||||||
|
name = serializers.CharField()
|
||||||
|
count = serializers.IntegerField(required=False)
|
||||||
|
|
||||||
|
|
||||||
class TgItemSerializer(serializers.ModelSerializer):
|
class TgItemSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = TgItem
|
model = TgItem
|
||||||
|
@ -37,7 +43,10 @@ class TgStatItemSerializer(serializers.Serializer):
|
||||||
if not obj.get("location__parent"):
|
if not obj.get("location__parent"):
|
||||||
return None
|
return None
|
||||||
if isinstance(obj.get("location__parent"), list):
|
if isinstance(obj.get("location__parent"), list):
|
||||||
|
location_parent_ids = obj.get("location__parent")
|
||||||
queryset = Territory.objects.filter(id__in=obj.get("location__parent"))
|
queryset = Territory.objects.filter(id__in=obj.get("location__parent"))
|
||||||
|
for q in queryset:
|
||||||
|
q.count = location_parent_ids.count(q.id)
|
||||||
serializer = TerritorySerializer(queryset, many=True)
|
serializer = TerritorySerializer(queryset, many=True)
|
||||||
else:
|
else:
|
||||||
queryset = Territory.objects.get(id=obj.get("location__parent"))
|
queryset = Territory.objects.get(id=obj.get("location__parent"))
|
||||||
|
|
|
@ -147,13 +147,15 @@ class TmcStatViewset(viewsets.ViewSet):
|
||||||
.values("tmc__tmc_id")
|
.values("tmc__tmc_id")
|
||||||
.annotate(
|
.annotate(
|
||||||
inv_count=Count("location__parent"),
|
inv_count=Count("location__parent"),
|
||||||
tmc_count=Count("tmc__tmc_id", distinct=True)
|
tmc_count=Count("id", distinct=True)
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
location__parent=ArrayAgg("location__parent"),
|
location__parent=ArrayAgg("location__parent"),
|
||||||
tmc=ArrayAgg("tmc"),
|
tmc=ArrayAgg("tmc"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# logger.info(queryset)
|
logger.info(queryset)
|
||||||
|
# logger.info(TgItem.objects.all().values())
|
||||||
serializer = TgStatItemSerializer(queryset, many=True)
|
serializer = TgStatItemSerializer(queryset, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
# return Response()
|
||||||
|
|
Loading…
Reference in New Issue