MCPcopy
hub / github.com/django/django / create_default_site

Function create_default_site

django/contrib/sites/management.py:11–47  ·  view source on GitHub ↗
(
    app_config,
    verbosity=2,
    interactive=True,
    using=DEFAULT_DB_ALIAS,
    apps=global_apps,
    **kwargs,
)

Source from the content-addressed store, hash-verified

9
10
11def 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)

Callers 7

test_basicMethod · 0.90
test_multi_dbMethod · 0.90
test_save_anotherMethod · 0.90
test_custom_site_idMethod · 0.90
test_no_site_idMethod · 0.90

Calls 10

no_styleFunction · 0.90
cursorMethod · 0.80
SiteClass · 0.70
get_modelMethod · 0.45
allow_migrate_modelMethod · 0.45
existsMethod · 0.45
usingMethod · 0.45
saveMethod · 0.45
sequence_reset_sqlMethod · 0.45
executeMethod · 0.45

Tested by 7

test_basicMethod · 0.72
test_multi_dbMethod · 0.72
test_save_anotherMethod · 0.72
test_custom_site_idMethod · 0.72
test_no_site_idMethod · 0.72