get class name for empty queryset in django

前端 未结 4 362
梦如初夏
梦如初夏 2021-01-03 18:09

I have empty queryset of model Student

students = Students.objects.all()

If the above queryset is empty, then how can i get the mo

相关标签:
4条回答
  • 2021-01-03 18:30

    To get the model name from queryset

    queryset.__dict__['model'].__name__
    
    0 讨论(0)
  • 2021-01-03 18:31

    You can do:

    students.model.__name__
    >>> `Students`
    
    0 讨论(0)
  • 2021-01-03 18:32
    students.model
    

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

    0 讨论(0)
  • 2021-01-03 18:45
    >>> 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'
    
    0 讨论(0)
提交回复
热议问题