You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

management.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. Creates the default Site object.
  3. """
  4. from django.apps import apps as global_apps
  5. from django.conf import settings
  6. from django.core.management.color import no_style
  7. from django.db import DEFAULT_DB_ALIAS, connections, router
  8. def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs):
  9. try:
  10. Site = apps.get_model('sites', 'Site')
  11. except LookupError:
  12. return
  13. if not router.allow_migrate_model(using, Site):
  14. return
  15. if not Site.objects.using(using).exists():
  16. # The default settings set SITE_ID = 1, and some tests in Django's test
  17. # suite rely on this value. However, if database sequences are reused
  18. # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that
  19. # the next id will be 1, so we coerce it. See #15573 and #16353. This
  20. # can also crop up outside of tests - see #15346.
  21. if verbosity >= 2:
  22. print("Creating example.com Site object")
  23. Site(pk=getattr(settings, 'SITE_ID', 1), domain="example.com", name="example.com").save(using=using)
  24. # We set an explicit pk instead of relying on auto-incrementation,
  25. # so we need to reset the database sequence. See #17415.
  26. sequence_sql = connections[using].ops.sequence_reset_sql(no_style(), [Site])
  27. if sequence_sql:
  28. if verbosity >= 2:
  29. print("Resetting sequence")
  30. with connections[using].cursor() as cursor:
  31. for command in sequence_sql:
  32. cursor.execute(command)