41

我有模型学生的空查询集

students = Students.objects.all()

如果上面的查询集是空的,那么我怎样才能得到模型(类名)?

如何获取空查询集的模型名称?

编辑:

如何从查询集中获取应用名称?

4

4 回答 4

64
>>> students = Students.objects.all()

# The queryset's model class:
>>> students.model
project.app.models.Student

# Name of the model class:
>>> students.model.__name__
'Student'

# Import path of the models module:
>>> students.model.__module__
'project.app.models'

# Django app name:
>>> students.model._meta.app_label
'app'
于 2013-01-23T19:29:33.900 回答
8
students.model

查询集有一个model属性,可用于检索与其关联的模型。

于 2013-01-23T19:28:20.193 回答
2

你可以做:

students.model.__name__
>>> `Students`
于 2013-01-23T19:29:58.987 回答
0

从查询集中获取模型名称

queryset.__dict__['model'].__name__
于 2020-08-28T03:53:02.610 回答