Django orm: use annotate case on prefetch result

this is a near replica of my models:

class To(models.Model):
   pass

class FromA(models.Model):
    to = models.ForeignKey(To)

class FromB(models.Model):
    to = models.ForeignKey(To)

is there a way to write a query like this?

To.objects.annotate(from=Case(
                            When(froma__isnull=False, then=Prefetch("froma")),
                            When(fromb__isnull=False, then=Prefetch("fromb"))
                    ))
Back to Top