好吧,让django为您完成处理应用程序和模型的重任:
from pathlib import Path
from django.apps import apps
from django.conf import settings
project_root = Path(settings.BASE_DIR)
# Loop through all apps
for app_name, app_conf in sorted(apps.app_configs.items()):
# Local app within project_root?
try:
Path(app_conf.path).relative_to(project_root)
except ValueError:
# Not a local app => skip
continue
# Loop through all models of app_name
for model_name, model_cls in sorted(apps.all_models[app_name].items()):
print(f'App: {app_name:<25s} Model: {model_name}')
# Now work with the model ..
# .. create documentation
# .. export documentation
这应该会给你的任务一个开始。