"""Generic "is this row referenced?" check. Every model that later points at a customer, contact or billing unit with a ForeignKey (jobs, sales, invoices …) is picked up automatically through ``_meta.related_objects``, so this module never needs to know those modules. """ from django.db import models def is_referenced(instance: models.Model, *, ignore: tuple[str, ...] = ()) -> bool: for relation in instance._meta.related_objects: if relation.get_accessor_name() in ignore: continue related_model = relation.related_model if relation.many_to_many: if getattr(instance, relation.get_accessor_name()).exists(): # type: ignore[arg-type] return True continue if related_model._default_manager.filter(**{relation.field.name: instance}).exists(): return True return False