| 531 | } |
| 532 | |
| 533 | def to_representation(self, instance): |
| 534 | include_streams = self.context.get("include_streams", False) |
| 535 | |
| 536 | if include_streams: |
| 537 | self.fields["streams"] = serializers.SerializerMethodField() |
| 538 | return super().to_representation(instance) |
| 539 | else: |
| 540 | # Read from the prefetched channelstream_set (ordered by the |
| 541 | # viewset's Prefetch); chaining .order_by() rebuilds the |
| 542 | # queryset and fires one SELECT per row in list responses. |
| 543 | representation = super().to_representation(instance) |
| 544 | if "streams" in representation: |
| 545 | representation["streams"] = [ |
| 546 | cs.stream_id for cs in instance.channelstream_set.all() |
| 547 | ] |
| 548 | return representation |
| 549 | |
| 550 | def get_logo(self, obj): |
| 551 | return LogoSerializer(obj.logo).data |