Last active
October 10, 2015 04:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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