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