Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get class name for empty queryset in django

I have empty queryset of model Student

students = Students.objects.all()

If the above queryset is empty, then how can i get the model(class name)?

How can i get the model name for empty queryset?

EDIT:

How can i get the app name from the queryset?

like image 883
Asif Avatar asked Jan 23 '13 19:01

Asif


3 Answers

>>> 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'
like image 59
Jakub Roztocil Avatar answered Nov 15 '22 01:11

Jakub Roztocil


students.model

Querysets have a model attribute that can be used to retrieve the model they are associated with.

like image 9
mipadi Avatar answered Nov 15 '22 02:11

mipadi


You can do:

students.model.__name__
>>> `Students`
like image 2
Aamir Rind Avatar answered Nov 15 '22 01:11

Aamir Rind