Django 2.2를 사용하고 있으며 데이터베이스 테이블에 조명기를로드하는 사용자 지정 작업 (앱용)을 추가하기 위해 작성하고 싶습니다.
이것이 내가 지금까지 가지고있는 것입니다.
from django.core.management.base import BaseCommand
from django.core import management
class Command(BaseCommand):
help = 'Generate Capture Data from loaded fixtures'
def add_arguments(self, parser):
parser.add_argument('definitions', type=str, help='Name of the definitions fixtures file', default='definitions.json')
parser.add_argument('sections', type=str, help='Name of the sections fixtures file', default='sections.json')
parser.add_argument('questions', type=str, help='Name of the questions fixtures file', default='questions.json')
def handle(self, *args, **kwargs):
print(kwargs)
그러나을 실행 python manage.py generate_data
하면 다음 예외가 발생합니다.
usage: manage.py generate_data [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback]
[--no-color] [--force-color]
definitions sections questions
manage.py generate_data: error: the following arguments are required: definitions, sections, questions
내가 제공하는 기본값 add_arguments()
이 무시되는 이유는 무엇 입니까?
nargs 를 사용해야 할 것입니다 . 문서에 따르면 사례 중 하나 :
'?'. 가능한 경우 하나의 인수가 명령 줄에서 사용되며 단일 항목으로 생성됩니다. 명령 줄 인수가 없으면 기본값의 값이 생성됩니다.
따라서 코드는 다음과 같습니다.
parser.add_argument('definitions', nargs='?', type=str, help='Name of the definitions fixtures file', default='definitions.json')
parser.add_argument('sections', nargs='?', type=str, help='Name of the sections fixtures file', default='sections.json')
parser.add_argument('questions', nargs='?', type=str, help='Name of the questions fixtures file', default='questions.json')
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다