Annotate the channels queryset with `effective_*` columns that resolve to the override value when set, otherwise fall back to the channel's own value. Always eagerly loads the override one-to-one to avoid N+1 when the caller reads annotated attributes and then the related override.
(queryset, select_related_fks=False)
| 26 | |
| 27 | |
| 28 | def with_effective_values(queryset, select_related_fks=False): |
| 29 | """ |
| 30 | Annotate the channels queryset with `effective_*` columns that resolve to |
| 31 | the override value when set, otherwise fall back to the channel's own |
| 32 | value. Always eagerly loads the override one-to-one to avoid N+1 when the |
| 33 | caller reads annotated attributes and then the related override. |
| 34 | |
| 35 | Pass `select_related_fks=True` when the output path will access FK objects |
| 36 | through the `effective_*_obj` Channel properties; this pulls the override's |
| 37 | logo, channel_group, epg_data, and stream_profile in the same query so |
| 38 | those accessors do not trigger per-row lookups. |
| 39 | """ |
| 40 | annotations = { |
| 41 | f"effective_{field}": Coalesce( |
| 42 | f"override__{field}", |
| 43 | field, |
| 44 | ) |
| 45 | for field in OVERRIDABLE_FIELDS |
| 46 | } |
| 47 | qs = queryset.select_related("override").annotate(**annotations) |
| 48 | if select_related_fks: |
| 49 | qs = qs.select_related( |
| 50 | "override__logo", |
| 51 | "override__channel_group", |
| 52 | "override__epg_data", |
| 53 | "override__stream_profile", |
| 54 | ) |
| 55 | return qs |
no outgoing calls