Django

Custom Django Commands

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

18 câu hỏi phỏng vấn·
Senior
1

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

Câu trả lời

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?

Câu trả lời

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?

Câu trả lời

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 câu hỏi phỏng vấn

Nắm vững Django cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí