마이그레이션에 수퍼유저가 없으면 Django가 슈퍼유저를 생성합니다.
I have a migration that creates a superuser if it does not exist. <code>def create_superuser(apps, schema_editor): User = apps.get_model('auth', 'User') if not User.objects.filter(username='admin').exists(): User.objects.create_superuser('admin', 'admin@example.com', 'password') class Migration(migrations.Migration): dependencies = [ ('myapp', '0001_initial'), ] operations = [ migrations.RunPython(create_superuser), ] </code>
첫 번째 줄은 아직 존재하지 않는 경우 수퍼유저를 생성하는 함수를 생성합니다.
두 번째 줄은 'auth' 앱에서 User 모델을 가져옵니다.
세 번째 줄은 사용자 이름이 'admin'인 사용자가 있는지 확인합니다. 그렇지 않다면
네 번째 줄은 사용자 이름이 'admin', 이메일 주소가 'admin@example.com', 비밀번호가 'password'인 수퍼유저를 생성합니다.
다섯 번째와 여섯 번째 줄은 마이그레이션 클래스를 만들고 'myapp' 앱의 마이그레이션 '0001_initial'에 종속되도록 지정합니다.
일곱 번째 줄은 마이그레이션이 'create_superuser' 함수를 실행해야 함을 지정합니다.
슈퍼유저란?
수퍼유저는 Django 사이트에서 관리 권한을 가진 사용자입니다. 모델, 보기 및 애플리케이션을 만들고 관리하는 것과 같은 작업을 수행할 수 있습니다.