Skip to content

Instantly share code, notes, and snippets.

@luzfcb
Last active October 10, 2015 04:08
Show Gist options
  • Save luzfcb/3629920 to your computer and use it in GitHub Desktop.
Save luzfcb/3629920 to your computer and use it in GitHub Desktop.
Registra no admin automaticamente todos os models de todas as apps descritas no settings.py
#Registra no admin automaticamente todos os models de todas as apps descritas
#nos settings.py
#
#Baseado em http://djangosnippets.org/snippets/2066/
#Modificado por: Fabio C. Barrioneuvo da Luz
#2012-09-05
#https://gist.github.com/3629920
#Coloque este codigo no arquivo __ini__.py do modulo que contem o settings.py do seu projeto
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered
from django.db.models import get_app, get_models
from NOME_DO_SEU_PROJETO_DJANGO.settings import INSTALLED_APPS
def auto_register_all_apps():
for absolute_app_name in INSTALLED_APPS:
if 'django' not in absolute_app_name:
app_name = absolute_app_name.split('.')[-1]
app_models = get_app(app_name)
for model in get_models(app_models):
try:
admin.site.register(model)
except AlreadyRegistered:
pass
auto_register_all_apps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment