2 Creates the default Site object.
5 from django
.db
.models
import signals
6 from django
.db
import connections
7 from django
.db
import router
8 from django
.contrib
.sites
.models
import Site
9 from django
.contrib
.sites
import models
as site_app
10 from django
.core
.management
.color
import no_style
12 def create_default_site(app
, created_models
, verbosity
, db
, **kwargs
):
13 # Only create the default sites in databases where Django created the table
14 if Site
in created_models
and router
.allow_syncdb(db
, Site
) :
15 # The default settings set SITE_ID = 1, and some tests in Django's test
16 # suite rely on this value. However, if database sequences are reused
17 # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that
18 # the next id will be 1, so we coerce it. See #15573 and #16353. This
19 # can also crop up outside of tests - see #15346.
21 print "Creating example.com Site object"
22 Site(pk
=1, domain
="example.com", name
="example.com").save(using
=db
)
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
[db
].ops
.sequence_reset_sql(no_style(), [Site
])
29 print "Resetting sequence"
30 cursor
= connections
[db
].cursor()
31 for command
in sequence_sql
:
32 cursor
.execute(command
)
34 Site
.objects
.clear_cache()
36 signals
.post_syncdb
.connect(create_default_site
, sender
=site_app
)