Skip to content

Instantly share code, notes, and snippets.

@bkvirendra
Last active March 22, 2018 16:54
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 bkvirendra/419db59e9a069b3e252dbdf07d7d4d17 to your computer and use it in GitHub Desktop.
Save bkvirendra/419db59e9a069b3e252dbdf07d7d4d17 to your computer and use it in GitHub Desktop.
Generate django `apps.py` for all apps (in a project which didn't have it before)
import os
# this is only used if all your django apps are located in a sub directory
base_dir = 'apps/'
all_files = [base_dir + item for item in os.listdir(base_dir)]
app_dirs = [item for item in all_files if os.path.isdir(item)]
print(app_dirs)
for app in app_dirs:
app_name = app.split('/')[1]
# generate the apps.py file
with open(app + '/apps.py', 'w') as f:
f.write('''\
from django.apps import AppConfig
class %sConfig(AppConfig):
name = 'apps.%s'
''' % (app_name.title(), app_name))
# append python path to __init__
with open(app + '/__init__.py', 'a') as init_file:
init_file.write("\n" + "default_app_config = 'apps.%s.apps.%sConfig'" % (app_name, app_name.title()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment