Skip to content

Instantly share code, notes, and snippets.

@om2c0de
Forked from kissgyorgy/get_models_from_app.py
Created January 29, 2021 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save om2c0de/a5053bd9abe147474ebed334224752ab to your computer and use it in GitHub Desktop.
Save om2c0de/a5053bd9abe147474ebed334224752ab to your computer and use it in GitHub Desktop.
Django: Get list of models from app
# http://stackoverflow.com/questions/8702772/django-get-list-of-models-in-application
from django.db.models import get_app, get_models
app = get_app('my_application_name')
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
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment