When I need to test if an object isn’t reffered to anymore I could use
class ModelA(models.Model):
pass
class ModelB(models.Model):
retired = models.BooleanField(default=False)
modelA = models.ForeignKey(
ModelA,
)
ModelA.objects.filter(~Q(modelb__isnull=False))
The “weird” format of using negation and testing for False makes sure an Exists statement is created instead of a subquery or join.
However how would I test if “ModelA is only used in instances of ModelB that have retired=True”, or are not referred to at all.