2012-08-25 3 views
5

Django 관리 명령 설명서는 app/management/commands 폴더에서 생성되는 모든 명령을 보여줍니다. app/management/commands/install 및 app/management/commands/maintenance와 같은 하위 폴더에 명령을 넣을 수 있습니까? 어떻게이 일을 끝낼 수 있습니까?Django : 하위 폴더로 관리 명령을 분할

+1

문제에 대한 자세한 정보를 제공 할 수 있습니까? 그것은 당신이 찾고있는 것입니까? https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ – Marat

+0

app/management/명령의 하위 폴더에 명령을 입력하고 싶습니다. 질문에 더 자세히 설명했습니다. –

답변

5

불행히도 Django 1.4부터는 그렇게 할 방법이없는 것으로 보입니다. 당신이 볼 수 있듯이

def find_commands(management_dir): 
    """ 
    Given a path to a management directory, returns a list of all the command 
    names that are available. 

    Returns an empty list if no commands are defined. 
    """ 
    command_dir = os.path.join(management_dir, 'commands') 
    try: 
     return [f[:-3] for f in os.listdir(command_dir) 
       if not f.startswith('_') and f.endswith('.py')] 
    except OSError: 
     return [] 

, 그것은 단지 하위 폴더를 무시하고 직접 commands 폴더 안에 파일을 고려 : django.core.management.__init__.py에 대한 소스는이 방법을 가지고있다. 실제로 Command 인스턴스를 생성하는 코드는 다음이기 때문에, 당신은 "원숭이 패치"어떻게 든이 기능을, 나머지 코드는 잘 작동합니다 경우 : 그래서

def load_command_class(app_name, name): 
    """ 
    Given a command name and an application name, returns the Command 
    class instance. All errors raised by the import process 
    (ImportError, AttributeError) are allowed to propagate. 
    """ 
    module = import_module('%s.management.commands.%s' % (app_name, name)) 
    return module.Command() 

당신이 한 경우, 명령은 subfolder.command에게 그것을 이름 올바른 스크립트를로드하고 올바른 클래스를 인스턴스화합니다.

실용적인 관점에서 볼 때, 나는 그것을 사용하지 않습니다. 물론 "namespace'd"명령을 사용하는 것이 좋겠지 만 원하는 경우 다른 명령을 구분 기호 (예 : _)를 사용하여 원하는 경우 모든 명령 앞에 항상 접두사를 붙일 수 있습니다. 명령 이름 길이 - 터미널에 입력하는 데 필요한 키 입력 횟수는 동일합니다 ...

관련 문제