Django get list of models in application
From Django 1.7 on, you can use this code, for example in your admin.py to register all models:
from django.apps import appsfrom django.contrib import adminfrom django.contrib.admin.sites import AlreadyRegisteredapp_models = apps.get_app_config('my_app').get_models()for model in app_models: try: admin.site.register(model) except AlreadyRegistered: pass
UPDATE
for newer versions of Django check Sjoerd answer below
Original answer from 2012:This is the best way to accomplish what you want to do:
from django.db.models import get_app, get_modelsapp = get_app('my_application_name')for model in get_models(app): # do something with the model
In this example, model
is the actual model, so you can do plenty of things with it:
for model in get_models(app): new_object = model() # Create an instance of that model model.objects.filter(...) # Query the objects of that model model._meta.db_table # Get the name of the model in the database model._meta.verbose_name # Get a verbose name of the model # ...