Skip to content

Instantly share code, notes, and snippets.

@ruchej
Last active February 2, 2022 18:18
Show Gist options
  • Save ruchej/09e2d795f2333b87cd914d76d5bca211 to your computer and use it in GitHub Desktop.
Save ruchej/09e2d795f2333b87cd914d76d5bca211 to your computer and use it in GitHub Desktop.
from django.conf import settings
from django.db import migrations, transaction
def forwards_func(apps, schema_editor):
if settings.DEBUG:
# Only in DEBUG mode!
try:
Account = apps.get_model("accounts", "Account")
UserStatus = apps.get_model("accounts", "UserStatus")
with transaction.atomic():
Account.objects.create_superuser(
username="admin",
email="root@google.com",
password="admin",
status=UserStatus.MODERATOR,
)
except Exception as exp:
print(f"Cann't create super user for debug: {exp}")
def reverse_func(apps, schema_editor):
if settings.DEBUG:
# Only in DEBUG mode!
try:
Account = apps.get_model("accounts", "Account")
with transaction.atomic():
Account.objects.all().delete()
except Exception as exp:
print(f"Cann't delete super user for debug: {exp}")
class Migration(migrations.Migration):
dependencies = [("core", "0001_initial")]
operations = [migrations.RunPython(forwards_func, reverse_func)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment