如何在djang中从self中获取模型名称

2024-04-26 03:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的视图.py,我想从self获取型号名称

我试过这个self.__class__.__name__

但它给了我视图的类名,而不是模型

编辑:

class ObjectCreate(CreateView):

    def get_template_names(self):
        m = self.__class__.__name__

Tags: namepy模型self名称视图编辑get
1条回答
网友
1楼 · 发布于 2024-04-26 03:26:07

可以这样做,在模型视图中,self不是对象模型实例。在

你应该写:

self.model.__name__

编辑6年后到期ihhcarus评论

For Django 1.11 you can use self.__class__.__name__ to get the model class name.

相关问题 更多 >