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

Is there a place in Django where I can get a list or find models that ORM knows about?

+73
django django-models
Jul 14 '09 at 12:40
source share
7 answers

A simple solution:

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

By default, apps.get_models() does not include

  • automatically generated models for many-to-many relationships without an explicit staging table
  • models that have been replaced.

If you want to enable them,

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

Prior to Django 1.7, use:

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

The include_auto_created parameter also provides retrieval from tables implicitly created by ManyToManyField .

+152
Jul 14 '09 at 15:32
source share

List of models using http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

 from django.contrib.contenttypes.models import ContentType for ct in ContentType.objects.all(): m = ct.model_class() print "%s.%s\t%d" % (m.__module__, m.__name__, m._default_manager.count()) 
+9
Feb 23 2018-12-12T00:
source share

If you want to play rather than use a good solution , you can play around with python introspection a bit:

 import settings from django.db import models for app in settings.INSTALLED_APPS: models_name = app + ".models" try: models_module = __import__(models_name, fromlist=["models"]) attributes = dir(models_module) for attr in attributes: try: attrib = models_module.__getattribute__(attr) if issubclass(attrib, models.Model) and attrib.__module__== models_name: print "%s.%s" % (models_name, attr) except TypeError, e: pass except ImportError, e: pass 

Note: this is a pretty rough part of the code; he will assume that all models are defined in "models.py" and that they inherit from django.db.models.Model.

+4
Jul 14 '09 at 14:38
source share

If you use the contenttypes application, then this is simple: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

+1
Jul 14 '09 at 12:44
source share

If you register your models in the admin application, you can see all the attributes of these classes in the administrator documentation.

0
Jul 14. '09 at 15:40
source share

Here is an easy way to find and remove any permissions that exist in the database but do not exist in the ORM model definitions:

 from django.apps import apps from django.contrib.auth.management import _get_all_permissions from django.contrib.auth.models import Permission from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **options): builtins = [] for klass in apps.get_models(): for perm in _get_all_permissions(klass._meta): builtins.append(perm[0]) builtins = set(builtins) permissions = set(Permission.objects.all().values_list('codename', flat=True)) to_remove = permissions - builtins res = Permission.objects.filter(codename__in=to_remove).delete() self.stdout.write('Deleted records: ' + str(res)) 
0
Jan 07 '19 at 20:02
source share

If you need a dictionary with all models, you can use:

 from django.apps import apps models = { model.__name__: model for model in apps.get_models() } 
0
May 26 '19 at 17:12
source share



All Articles