Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How can I find which of my models refer to a model

Tags:

django

I'd like to warn or prevent a user from deleting an object instance which is referred to by other instances. Is there a nice way to do this?

One way would be to get a list of models which include the referent and then try reverse look-ups on them. Is there a way to get that list of models? Or is there a better way?

While investigating the collector suggestion, I found some related information and wrote the following which finds the classes which have the referent as a foreign key:

def find_related(cl, app):
    """Find all classes which are related to the class cl (in app) by 
    having it as a foreign key."""

    from django.db import models

    all_models = models.get_models()
    ci_model = models.get_model(app, cl)
    for a_model in all_models:
        for f in a_model._meta.fields:
            if isinstance(f, ForeignKey) and (f.rel.to == ci_model):
                print a_model.__name__

Based on suggestion to use the code in collect:

def find_related(instance):
"""Find all objects which are related to instance."""

for related in instance._meta.get_all_related_objects():
    acc_name = related.get_accessor_name()
    referers = getattr(instance, acc_name).all()
    if referers:
        print related
like image 993
Mitch Avatar asked Sep 24 '11 13:09

Mitch


1 Answers

Django has something called Collector class. It is used by Django when performing a model deletion. What it does seems like exactly what you want. By calling collect() it finds all the references to the object in the model graph. Additionally it offers a way to delete all the found objects, with a delete() call.

That said I've never used this class myself, I just know it exists. The API is somewhat convoluted, but if you're willing to dig into the internals of Django a little bit, it might save you a lot of coding.

like image 51
julx Avatar answered Sep 25 '22 06:09

julx