我有模型学生的空查询集
students = Students.objects.all()
如果上面的查询集是空的,那么我怎样才能得到模型(类名)?
如何获取空查询集的模型名称?
编辑:
如何从查询集中获取应用名称?
我有模型学生的空查询集
students = Students.objects.all()
如果上面的查询集是空的,那么我怎样才能得到模型(类名)?
如何获取空查询集的模型名称?
编辑:
如何从查询集中获取应用名称?
>>> 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'
students.model
查询集有一个model
属性,可用于检索与其关联的模型。
你可以做:
students.model.__name__
>>> `Students`
从查询集中获取模型名称
queryset.__dict__['model'].__name__