PyCharm 2023.3 Help

Django Structure tool window

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

View the Django 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 App expui general more horizontal More tool windows and select Django Structure from the list.

  • The Django Structure tool window lists the following components:

    • App expui nodes data tables Models

    • App expui general settings Admin classes

    • App expui general preview only Views

  • By default, the components are grouped by apps. To group them by component type, click App client expui general show 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

Managing apps

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

Create an app

  1. 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.

    Creating a Django app in the Django Structure tool window
  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: 02 January 2024