是的,你可以,這些命令被稱爲管理命令,你可以寫下面的指南中的文檔Writing custom django-admin commands
要求用戶輸入可以input()
函數來完成一個:
#/django/contrib/auth/management/commands/createsuperuser.py
from django.utils.six.moves import input
def get_input_data(self, field, message, default=None):
"""
Override this method if you want to customize data inputs or
validation exceptions.
"""
raw_value = input(message)
if default and raw_value == '':
raw_value = default
try:
val = field.clean(raw_value, None)
except exceptions.ValidationError as e:
self.stderr.write("Error: %s" % '; '.join(e.messages))
val = None
return val
完整的源代碼可以在Github上找到Django's createsuperuser。