djangodjango-extensions

Add method imports to shell_plus with alias


I have a model naming collision in my django models, with 2 models called Tag in separate modules. That looks something like this:

.
├── conversations/
│   └──models.py           <- contains a model called Tag
└── dogs/
    └──models.py           <- contains a model called Tag

In shell_plus I want to import each module with an alias in SHELL_PLUS_POST_IMPORTS so that it's available when I start the shell. How can I do this?

As an example, if i were doing this manually in shell_plus I'd write:

# These are the imports I want to happen automatically:
from conversation import models as conversation_models
from dogs import models as dog_models

# Then I can write:
conversation_models.Tag()
dog_models.Tag()

Solution

  • In your settings you can specify additional imports:

    SHELL_PLUS_IMPORTS = [
        'from conversations.models import Tag as ConversationsTag',
        'from dogs.models import Tag as DogsTag',
    ]