App Setup Improvements

Automating the creation of a superuser

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $15

New files

app/migrations/0002_auto_20241113_1653.py
# Generated by Django 5.1.3 on 2024-11-13 16:53

from django.utils import timezone
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0001_initial'),
    ]

    def create_superuser(apps, schema_editor):
        from django.contrib.auth import get_user_model

        User = get_user_model()

        if User.objects.exists():
            return

        superuser = User.objects.create_superuser(
            username="example_username",
            email="example@example.com",
            password="example_password",
            last_login=timezone.now()
        )
        superuser.save()

    operations = [
        migrations.RunPython(create_superuser)
    ]