Junior — Middle
Have you created custom management commands in Django to extend functionality via command line?
sobes.tech AI
Answer from AI
Yes, in Django, you can create custom management commands to extend functionality via the command line. To do this:
- Create a folder named
management/commandsin your app. - Inside it, create a file with the name of your command, for example,
mycommand.py. - In this file, define a class inheriting from
BaseCommand. - Override the
handle(self, *args, **options)method, where you implement the command logic.
Here's an example of a simple command:
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Example of a custom command'
def handle(self, *args, **options):
self.stdout.write('Command executed successfully!')
After that, you can run the command using python manage.py mycommand.