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 apps
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered
app_models = apps.get_app_config('my_app').get_models()
for model in app_models:
try:
admin.site.register(model)
except AlreadyRegistered:
pass
Best answer I found to get all models from an app:
from django.apps import apps
apps.all_models['<app_name>'] #returns dict with all models you defined
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_models
app = 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
# ...