Django : How can I find a list of models that the ORM knows?

前端 未结 7 1703
挽巷
挽巷 2020-12-04 08:48

In Django, is there a place I can get a list of or look up the models that the ORM knows about?

相关标签:
7条回答
  • 2020-12-04 09:42

    Simple solution:

    import django.apps
    django.apps.apps.get_models()
    

    By default apps.get_models() don't include

    • auto-created models for many-to-many relations without an explicit intermediate table
    • models that have been swapped out.

    If you want to include these as well,

    django.apps.apps.get_models(include_auto_created=True, include_swapped=True)
    

    Prior to Django 1.7, instead use:

    from django.db import models
    models.get_models(include_auto_created=True)
    

    The include_auto_created parameter ensures that through tables implicitly created by ManyToManyFields will be retrieved as well.

    0 讨论(0)
提交回复
热议问题