且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django:我如何找到我的模型中的哪个模型

更新时间:2023-11-30 17:33:04

Django有一个叫收藏者类。 Django在执行模型删除时使用它。它的作用似乎是你想要的。通过调用 collect(),它会在模型​​图中找到对对象的所有引用。此外,它还提供了一种删除所有找到的对象的方法,一个 delete()调用。

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.

说我从来没有使用过这个类,我只知道它存在。 API有点复杂,但如果您愿意一点点挖掘Django的内部结构,那么可能会为您节省大量的编码。

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.