Management Commands¶
djangocms-versioning provides management commands to help with version management and maintenance.
create_versions¶
Creates Version objects for versioned content that does not have a version assigned. This command is typically used:
During initial setup of versioning on existing content
After migrations if something goes wrong
As a recovery tool if Version objects are missing
When to Use¶
Use this command in these scenarios:
Initial versioning setup: You have existing content in your database and you’re adding versioning support.
After migrations: If a migration fails or is rolled back, leaving content without Version objects.
Recovery from data loss: If Version objects have been accidentally deleted (when
DJANGOCMS_VERSIONING_ALLOW_DELETING_VERSIONSis True).Multi-app versioning: When adding versioning to additional models after the initial setup.
Basic Usage¶
# Create versions with default settings
python manage.py create_versions --userid 1
# Create versions as a specific user
python manage.py create_versions --username admin
# Test without making changes (dry-run)
python manage.py create_versions --userid 1 --dry-run
Command Options¶
Option |
Description |
|---|---|
|
State to assign to newly created versions (default: draft). Cannot be “unpublished” |
|
Username of the user who will be the author of created versions |
|
User ID of the user who will be the author of created versions |
|
Preview what would happen without making changes to the database |
|
Verbosity level |
State Assignment Logic¶
The command intelligently assigns states to versions:
If no versions exist for a grouper, content gets the requested state (default:
draft)If a version already exists with the requested state, new content is assigned
archivedOnly one version per grouper can have
draftorpublishedstate at a time
Example: You have a Post with one existing published version. Running:
python manage.py create_versions --state draft --userid 1
Would create a version with state archived (not draft) because draft must be unique.
User Specification¶
You must specify who created the versions. In order of precedence:
DJANGOCMS_VERSIONING_DEFAULT_USER setting (if set, cannot be overridden)
–userid option (command line user ID)
–username option (command line username)
If none are provided and there’s content to version, the command fails:
# This will fail if there's unversioned content and no default user is set
python manage.py create_versions
# Error: "Please specify a user which missing Version objects shall belong to"
Configuration Option¶
To avoid having to specify a user every time, set in your settings:
# settings.py
DJANGOCMS_VERSIONING_DEFAULT_USER = 1 # pk of the migration/default user
Then you can run:
python manage.py create_versions
Common Scenario¶
When adding versioning to an existing model with existing content:
# Create a migration user if you don't have one
python manage.py shell
>>> from django.contrib.auth import get_user_model
>>> User = get_user_model()
>>> migration_user = User.objects.create_user('migration', 'migration@example.com', 'password')
>>> print(migration_user.pk)
1
# Exit shell and first identify the changes
python manage.py create_versions --userid 1 --dry-run
# Then run create_versions
python manage.py create_versions --userid 1
# Or set it as default and run without specifying
python manage.py create_versions
Integrating with Migrations¶
You can call this command from a Django migration for automatic setup:
# yourapp/migrations/0005_add_versioning.py
from django.core.management import call_command
from django.db import migrations
def create_versions_for_migration(apps, schema_editor):
call_command('create_versions', userid=1)
class Migration(migrations.Migration):
dependencies = [
('yourapp', '0004_previous_migration'),
]
operations = [
migrations.RunPython(create_versions_for_migration),
]
Note
When using in migrations, it’s better to set DJANGOCMS_VERSIONING_DEFAULT_USER
in settings so you don’t have to hardcode user IDs in migrations.