---
url: "https://vueda.dev/v3/reference/api/py/vueda.user.management.commands.utils.md"
title: "commands.utils"
---

# commands.utils

## Overview

Utilities for group migration management commands.

## `call_management_command` {#call_management_command}

Run ``args`` through Django's ``call_command``, capturing its output.

``command`` is the ``BaseCommand`` instance invoking this (needs ``.dry_run``, ``.style``, ``.stdout``).
A trailing ``--dry-run`` is appended automatically when ``command.dry_run`` is set and the command
being run is ``makemigrations``. Exits the process with status 2 if the command wrote to stderr.

### Signature {#call_management_command-signature}

`call_management_command(command, args)`

#### Parameters {#call_management_command-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="call_management_command-param-command">command</span> |  | yes |  |  |
| <span id="call_management_command-param-args">args</span> |  | no |  |  |

### Source {#call_management_command-source}

`server/vueda/user/management/commands/utils.py:268`

## `create_group_change` {#create_group_change}

Create a ``group_change_model`` row from ``change``, keep its original ``when`` timestamp, and return its pk.

Generated group migrations copy this function into their source.

### Signature {#create_group_change-signature}

`create_group_change(change, group_change_model)`

#### Parameters {#create_group_change-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="create_group_change-param-change">change</span> |  | yes |  |  |
| <span id="create_group_change-param-group_change_model">group_change_model</span> |  | yes |  |  |

### Source {#create_group_change-source}

`server/vueda/user/management/commands/utils.py:231`

## `get_matching_record` {#get_matching_record}

Return GroupChange.pk if a record matching this change exists, None otherwise.

### Signature {#get_matching_record-signature}

`get_matching_record(change, group_change_model)`

#### Parameters {#get_matching_record-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="get_matching_record-param-change">change</span> |  | yes |  |  |
| <span id="get_matching_record-param-group_change_model">group_change_model</span> |  | yes |  |  |

### Source {#get_matching_record-source}

`server/vueda/user/management/commands/utils.py:215`

## `get_migration_names_from_show_migrations` {#get_migration_names_from_show_migrations}

Return the migration names for ``app_label`` by running ``showmigrations`` through ``command``.

Returns None when ``showmigrations`` prints nothing.

### Signature {#get_migration_names_from_show_migrations-signature}

`get_migration_names_from_show_migrations(command, app_label)`

#### Parameters {#get_migration_names_from_show_migrations-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="get_migration_names_from_show_migrations-param-command">command</span> |  | yes |  |  |
| <span id="get_migration_names_from_show_migrations-param-app_label">app_label</span> |  | yes |  |  |

### Source {#get_migration_names_from_show_migrations-source}

`server/vueda/user/management/commands/utils.py:323`

## `get_migrations_path` {#get_migrations_path}

Return the directory of the migrations package for ``app_config``, honoring ``MIGRATION_MODULES``.

Returns None when migrations are disabled, the module cannot be imported, or it is a single module.

### Signature {#get_migrations_path-signature}

`get_migrations_path(app_config)`

#### Parameters {#get_migrations_path-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="get_migrations_path-param-app_config">app_config</span> |  | yes |  |  |

### Source {#get_migrations_path-source}

`server/vueda/user/management/commands/utils.py:335`

## `has_direct_runpython_import` {#has_direct_runpython_import}

Return True if ``lines`` imports RunPython directly, e.g. ``from django.db.migrations import RunPython``,
rather than only accessing it via ``migrations.RunPython``.

``lines`` is a list of strings, as returned by ``readlines()``. Only module-level ``from ... import``
statements are considered, so a RunPython import nested inside a copied function source is never matched.

### Signature {#has_direct_runpython_import-signature}

`has_direct_runpython_import(lines)`

#### Parameters {#has_direct_runpython_import-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="has_direct_runpython_import-param-lines">lines</span> |  | yes |  |  |

### Source {#has_direct_runpython_import-source}

`server/vueda/user/management/commands/utils.py:252`

## `locate_empty_migration_slots` {#locate_empty_migration_slots}

Locate the line numbers of the key components of a freshly generated, empty migration.

``lines`` is a list of strings, as returned by ``readlines()``, for a migration file created while
``migrations.Migration.operations`` was forced to a known, findable shape (two ``RunPython`` calls
using ``dict``/``type`` and ``str``/``int`` as placeholder ``code``/``reverse_code`` values). Returns a
dict of 0-based line numbers: ``class_index`` (the ``class Migration`` line), ``dependencies_index``
(the ``dependencies = [...]`` assignment), ``import_start``/``import_end`` (the import block), and the
four placeholder keyword-value locations (``p_forwards_index``, ``p_reverse_index``, ``forwards_index``,
``reverse_index``). Raises ``RuntimeError`` if any of them can't be found, e.g. because Django changed
the shape of the empty-migration template it emits.

### Signature {#locate_empty_migration_slots-signature}

`locate_empty_migration_slots(lines)`

#### Parameters {#locate_empty_migration_slots-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="locate_empty_migration_slots-param-lines">lines</span> |  | yes |  |  |

### Source {#locate_empty_migration_slots-source}

`server/vueda/user/management/commands/utils.py:360`

## `parse_date_from_django_comment` {#parse_date_from_django_comment}

Return the UTC date in a migration's "Generated by Django ... on" comment, converted to local time.

### Signature {#parse_date_from_django_comment-signature}

`parse_date_from_django_comment(django_comment)`

#### Parameters {#parse_date_from_django_comment-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="parse_date_from_django_comment-param-django_comment">django_comment</span> |  | yes |  |  |

### Source {#parse_date_from_django_comment-source}

`server/vueda/user/management/commands/utils.py:314`

## `parse_migrations_from_show_migrations` {#parse_migrations_from_show_migrations}

Return the migration names listed in ``showmigrations`` output ``lines``, without their applied markers.

### Signature {#parse_migrations_from_show_migrations-signature}

`parse_migrations_from_show_migrations(lines)`

#### Parameters {#parse_migrations_from_show_migrations-parameters}

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| <span id="parse_migrations_from_show_migrations-param-lines">lines</span> |  | yes |  |  |

### Source {#parse_migrations_from_show_migrations-source}

`server/vueda/user/management/commands/utils.py:301`

## Source

`server/vueda/user/management/commands/utils.py`
