[django | command] Build a Backend REST API - 19
Add wait_for_db_command💗
以前に設定したソースコードはテストソースコードであり、実際のカスタムコマンドを実行するソースコードの作成を開始します.
making a structure🎭
core
アプリケーションの下
commandsフォルダとinitを含む管理フォルダを作成します.それぞれPyを作ろう
次にcommandsフォルダにinitします.pyを再作成し、wait_for_db.py
ファイルを生成
wait_for_db.pyの作成🎗
import time
from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand
class Command(BaseCommand):
"""Django command to pause execution until database is available"""
def handle(self, *args, **options):
self.stdout.write('Waiting for database...')
db_conn = None
while not db_conn:
try:
db_conn = connections['default']
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
STEP1 import🧧
初回導入time
2番目の次のDjangodbパッケージのconnections
モジュールをインポートします.
import time
from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand
class Command(BaseCommand):
"""Django command to pause execution until database is available"""
def handle(self, *args, **options):
self.stdout.write('Waiting for database...')
db_conn = None
while not db_conn:
try:
db_conn = connections['default']
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
from django.db.utils import OperationalError
4つ目はcustom command
バージョンのために「BaseCommand」をインポートimport time
from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand
STEP2 class Command🎡
BaseCommand
を継承し、Commandクラスを作成します.参考までにDocstringを書くのは常に良い習慣なので、短いドキュメントを書いてください.
また、Commnadクラスとhandleメソッドの名前は変更できません.これは私が倉庫で決めた名前だからです.
handle()
メソッドは、下図のように本文書に記載されている.実際の論理を実現する方法と考えられる.
BaseCommand.handle(*args, **options)
The actual logic of the command. Subclasses must implement this method.
It may return a Unicode string which will be printed to stdout (wrapped by BEGIN; and COMMIT; if output_transaction is True).
self.stdout.write('Waiting for database...')
コードは画面に情報を表示します.db connn変数を作成します.値はNoneです.
while not db_conn:
はdb connがNone値やステータス変更ではなく別の値に割り当てられたときに終了します.そうしないと、データベースが接続されていない場合は、ループ文の実行を続行します.try ... except ...
OperationalError
を見つけ出す文章を書きますconnection変数は
ConnectionHandler
というクラスを変数化したものですexcept OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
このセクションでは、エラーが発生した場合、通知画面がデバイス接続を確立できないことを通知し、初期ブリッジであることを通知します.そして1秒止まります
class Command(BaseCommand):
"""Django command to pause execution until database is available"""
def handle(self, *args, **options):
self.stdout.write('Waiting for database...')
db_conn = None
while not db_conn:
try:
db_conn = connections['default']
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
Reference
この問題について([django | command] Build a Backend REST API - 19), 我々は、より多くの情報をここで見つけました https://velog.io/@hyeseong-dev/django-command-Build-a-Backend-REST-API-19テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol