Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use get_or_create instead of catching exception

Adam Bogdał02a95bafbcf7
diff --git a/saleor/product/management/commands/populatedb.py b/saleor/product/management/commands/populatedb.py
index e8f9a25067..3befce6240 100644
--- a/saleor/product/management/commands/populatedb.py
+++ b/saleor/product/management/commands/populatedb.py
@@ -1,5 +1,4 @@
from django.core.management.base import BaseCommand
-from django.db import IntegrityError
from utils.create_random_data import create_items, create_users, create_orders
@@ -28,11 +27,14 @@ class Command(BaseCommand):
if options['createsuperuser']:
credentials = {'email': '[email protected]', 'password': 'admin'}
- try:
- User.objects.create_superuser(**credentials)
- except IntegrityError:
+ user, created = User.objects.get_or_create(
+ email=credentials['email'],
+ is_active=True, is_staff=True, is_superuser=True)
+ if created:
+ user.set_password(credentials['password'])
+ user.save()
self.stdout.write(
- 'Superuser already exists - %(email)s' % credentials)
+ 'Superuser - %(email)s/%(password)s' % credentials)
else:
self.stdout.write(
- 'Superuser - %(email)s/%(password)s' % credentials)
+ 'Superuser already exists - %(email)s' % credentials)