def unregister_referenced_index(sender, **kwargs): """Unregister a :class:`ObjectIndex` if there isn't any references to it.""" instance = kwargs['instance'] #get the key referencing the ObjectIndex class fkeys = foreign_keys(instance.__class__) fkeys.update(many_to_many_fields(instance.__class__)) index_key = None for key, details in fkeys.items() : if issubclass(details['class'], ObjectIndex) : index_key = key break #if found get the ObjectIndex instance #its reverse foreign keys and reverse many to many fields #then detect if some relationships remain #and finally delete the ObjectIndex instance #if it is not the case if index_key : index = getattr(instance, index_key) keys = reverse_foreign_keys(ObjectIndex).keys() keys.extend(reverse_many_to_many_fields(ObjectIndex).keys()) for key in keys : count = getattr(index, key).all() if count : return index.delete()
def get_dependencies(cls): """ Get fields materialising dependencies between 2 classes that could lead to a delete cascading. """ dct = SortedDict() dct.update(reverse_foreign_keys(cls)) dct.update(reverse_one_to_one_keys(cls)) dct.update(generic_relations(cls)) return dct