首页 文章

获取django中空查询集的类名

提问于
浏览
27

我有模型学生的空查询集

students = Students.objects.all()

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

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

编辑:

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

3 回答

  • 6
    >>> 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'
    
  • 40
    students.model
    

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

  • 2

    你可以做:

    students.model.__name__
    >>> `Students`
    

相关问题