PyCharm 2024.1 Help

Django Structure tool window

Enable the Django plugin

This functionality relies on the Django plugin, which is bundled and enabled in PyCharm by default. If the relevant features aren't available, make sure that you didn't disable the plugin.

  1. Press Ctrl+Alt+S to open settings and then select Plugins.

  2. Open the Installed tab, find the Django plugin, and select the checkbox next to the plugin name.

The Django Structure tool window provides a quick way to create, refactor and navigate the main components of your Django project.

View the project structure

  • Click Django Structure on the left to open the tool window.

    Django structure tool window

    If the tool window icon is not shown, click More tool windows and select Django Structure from the list.

  • The Django Structure tool window lists the following components:

    • Models

    • Admin classes

    • Views

    By default, only models are displayed. To enable other components, click View Options in the toolbar and select them in the Show section.

  • By default, the components are grouped by apps. To group them by component type, click View Options in the toolbar and select Components.

    For components with similar names, application names are also displayed.

    Django structure grouped by component type
  • To go to a component, double-click it. The containing file is opened in the editor, and the caret is set to the corresponding class declaration or function definition.

    Jumping to a Django component from the Django Structure tool window

Access the project settings

  • In the Django Structure tool window, double-click Settings Settings. The Django project settings file (for example, settings.py) is opened in the editor.

Managing apps

You can use the Django Structure tool window to create new apps and run migrations.

Create an app

  1. Do one of the following:

    • Click New Django App in the toolbar of the Django Structure tool window.

      Creating a Django app in the Django Structure tool window
    • Right-click an existing app. If there are no apps yet, click the Create App link.

      Select New Django App from the context menu.

  2. In the terminal that opens, specify the name of the app and press Enter.

    Specifying the app name in the manage.py console

Run migrations for an app

  1. Right-click the app in the Django Structure tool window and select Make Migrations from the context menu.

  2. In the terminal that opens, type the app name and press Enter

  3. Go back to the Django Structure tool window, right-click the app, and select Migrate from the context menu.

  4. In the terminal that opens, type the app name and press Enter

  • Right-click the app in the Django Structure tool window and select Open in Project View from the context menu.

  • The app directory is opened in the Project tool window.

    Opening the app directory in the Project tool window

Managing models

The Django Structure tool window lets you perform the following operations with models:

Create a model

  1. In the Django Structure tool window, right-click the Models node or any existing model.

  2. Select New Model from the context menu.

    Creating a new model in the Django Structure tool window
  3. If the app contains several files with models, for example, a directory or a Python module with models, PyCharm will ask you to choose the file for the new model.

    Choosing the location for the new model
  4. The file with models (for example, models.py) is opened in the editor.

    Specify the name of the new model and press Enter, then you can proceed by adding the fields and other model code instead of pass.

    New model is created in the file with models

Register a model in the admin interface

  1. Right-click the model in the Django Structure tool window and select Register in Django Admin from the context menu.

    Registering a model in the admin interface
  2. admin.py is opened in the editor, and a new model admin class with the @admin.register decorator is added.

    The model is registered in admin.py

    Press Enter to confirm the class creation.

When models are registered in the admin interface, the corresponding model admin classes are listed under the Admin node in the Django Structure tool window.

If your project contains model admin classes that have not been used to register models yet, they are grayed out in the Django Structure tool window.

Unused admin classes

You can use the context menu to register a model with such a model admin class.

Register a model with an unused admin

  1. Right-click the model admin class in the Django Structure tool window and select Register in Django Admin from the context menu.

    Registering a model with an unused model admin class
  2. Select the model in the popup. If there are no unregistered models, an All models already registered message appears.

    Selecting a model register with the admin class
  3. admin.py is opened in the editor, and an @admin.register decorator is added to the model admin class.

You can also use the context menu of model admin classes in the Admin node to find usages of model admin classes, refactor them, and navigate to the corresponding models.

  1. Right-click the model admin class in the Django Structure tool window and select Go to Model from the context menu.

    Navigating to a model from the model admin class
  2. If there are several models registered with the selected admin class, you will need to select the model in the popup.

    Selecting a model to navigate from the admin class

Refactor a model

  1. Right-click the model in the Django Structure tool window.

  2. In the context menu that opens, select Refactor and choose the refactoring from the list.

    Refactoring a model in the Django Structure tool window

Find usages of a model

  • Right-click the model in the Django Structure tool window and select Find Usages from the context menu.

    Finding usages of a model in the Django Structure tool window

    The Find tool window opens with the search results. For information, refer to Find usages in a project.

Managing views

The Django Structure tool window lets you perform the following operations with views:

Create a view

  1. In the Django Structure tool window, right-click the Views node or any existing view.

  2. Select New View from the context menu.

  3. Choose the view type.

    Choosing the view type
  4. If the app contains several files with views, PyCharm will ask you to choose the file for the new view.

  5. The file with views (for example, views.py) is opened in the editor.

    Specify the name of the new view and press Enter, then you can proceed by adding view code .

    New view is created in the file with views

Refactor a view

  1. Right-click the view in the Django Structure tool window.

  2. In the context menu that opens, select Refactor and choose the refactoring from the list.

    Refactoring a view in the Django Structure tool window

Find usages of a view

  • Right-click the view in the Django Structure tool window and select Find Usages from the context menu.

    Finding usages of a model in the Django Structure tool window

    The Find tool window opens with the search results. For information, refer to Find usages in a project.

Last modified: 17 June 2024