using union() with querysets that have a custom Prefetch() object

Do you think opening an issue on django bug tracker to support prefetch_related() querysets would be a good idea ?

Allowing prefetch_related to be called after union (as long as values and values_list are not used) should be acceptable (we demonstrated here that the check can be bypassed anyway) but implementing an automatic merge logic for union of prefetches is unlikely to be given the complexity involved.

Code wise this means allowing

Network.objects.filter(code="net1").union(
    Network.objects.filter(code="net2")
).prefetch_related(...)

is likely a good candidate but getting

Network.objects.filter(code="net1").prefetch_related(
    Prefetch(
        "station_set",
        queryset=Station.objects.filter(code="stat1")
    )
).union(
    Network.objects.filter(code="net2").prefetch_related(
        Prefetch(
            "station_set",
            queryset=Station.objects.filter(code="stat25")
        )
    )
)

to automatically get translated to

Network.objects.filter(code="net1").union(
    Network.objects.filter(code="net2")
).prefetch_related(
    Prefetch(
        "station_set",
        queryset=Station.objects.filter(
            Q(network__code="net1", code="stat1")
            | Q(network__code="net2", code="stat25")
        )
    )
)

likely isn’t given the complexity involved. In all cases ticket that requests clarifications on queryset combination (union, intersection, difference) interactions with prefetch_related would certainly be valuable.