Django

Custom Django Commands

Management commands, BaseCommand, argument parsing, scheduling, cron jobs, automation

18 면접 질문·
Senior
1

Where should a custom Django management command file be placed to be automatically detected?

답변

Custom Django management commands must be placed in the management/commands/ directory of a Django application. This directory must contain an __init__.py file in both management/ and commands/. Django automatically scans this path to discover available commands. The Python filename becomes the command name executable via manage.py.

2

Which base class should be used to create a custom Django management command?

답변

BaseCommand is the standard base class provided by django.core.management.base for creating custom commands. It provides the necessary infrastructure: argument parsing, output color handling, error management, and integration with manage.py. The handle() method must be implemented to contain the command logic.

3

How to define positional arguments in a custom Django management command?

답변

The add_arguments() method allows defining command arguments using the argparse parser. For positional arguments, use parser.add_argument('name') without a dash prefix. You can specify nargs for the number of expected arguments ('+' for one or more, '*' for zero or more). Arguments are then accessible via the options dictionary in handle().

4

Which method should be used to display a success message with appropriate colors in a Django command?

5

How to signal an error that stops Django command execution with a non-zero exit code?

+15 면접 질문

다음 면접을 위해 Django을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기