[Django]-Get class name for empty queryset in django

73👍

>>> 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'

9👍

students.model

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

👤mipadi

3👍

You can do:

students.model.__name__
>>> `Students`

1👍

To get the model name from queryset

queryset.__dict__['model'].__name__

0👍

If you want a model type which is a string:

queryset.model.__name__

You can check the type:

type(queryset.model.__name__)
>>> str

And if you want models.base.ModelBase type:

queryset.model._meta.model

You can check the type:

type(queryset.model._meta.model)                                                                                                                                             
>>> django.db.models.base.ModelBase
👤Keval

Leave a comment