Last active
March 22, 2018 16:54
-
-
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)
This file contains hidden or 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
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