ベースコマンドで作成したプログラム(例 : APIからデータを取得しモデルに登録)を実行したときの成功・エラーの状況などをエラー管理モデルに登録するプログラムです。
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
from django.core.management.base import BaseCommand from django.utils import timezone from AppName.models import ErrorManager # ErrorManagerモデルのインポート class Command(BaseCommand): help = 'Your command description' def handle(self, *args, **options): try: # メイン処理 Start # APIからデータをダウンロードしてBookモデルに登録 # メイン処理 End # 処理が成功した場合の記録 self.log_event( action_name='Your Action Name', dl_source_name='Source Name', dl_source_url='http://example.com', storage_location='/path/to/storage', error_flag=0, # エラーなしを示すフラグ error_datetime=timezone.now() ) except Exception as e: # エラーが発生した場合の記録 self.log_event( action_name='Your Action Name', dl_source_name='Source Name', dl_source_url='http://example.com', storage_location='/path/to/storage', error_flag=1, # エラーありを示すフラグ error_datetime=timezone.now() ) def log_event(self, **kwargs): # ErrorManagerモデルに情報を保存 ErrorManager.objects.create(**kwargs) |