说my_instance是榜样MyModel。
我正在寻找一种好方法:
my_model = get_model_for_instance(my_instance)
我还没有找到任何真正直接的方法来做到这一点。到目前为止,我已经提出了:
from django.db.models import get_model
my_model = get_model(my_instance._meta.app_label, my_instance.__class__.__name__)
这可以接受吗?它甚至是肯定可行的最佳实践方法吗?
还有_meta.object_name似乎提供与相同的东西__class__.__name__。可以?是好是坏?如果是这样,为什么?
另外,如果应用程序标签在项目范围内多次出现,例如如何从“ django.contrib.auth”获得“ auth”,并且还存在“ myproject.auth”,我怎么知道我得到的模型正确?
这样的情况get_model会变得不可靠吗?
感谢您的任何提示/提示和经验分享!
__class__绝对必须是该实例的类。没有任何歧义。这就是Python的工作方式。你在问什么?