3

According to the Django documentation (here):

django.contrib.sites registers a post_migrate signal handler which creates a default site named example.com with the domain example.com. This site will also be created after Django creates the test database. To set the correct name and domain for your project, you can use a data migration.

Isn't it safe to directly change the default values from the Django admin panel? why?

4
  • I just change the default site object to the correct information when setting up a site. Commented Jan 15, 2021 at 9:40
  • thank you @markwalker_, our website has worked for years and I'm not sure whether it's safe to just change those values or not. there are many relations in our database too. Commented Jan 15, 2021 at 9:45
  • actually, my knowledge doesn't have any doubt here, but the official document made me confused. Commented Jan 15, 2021 at 9:48
  • 1
    You'll be fine to just edit the default site. It gets used by various things internally. Things like the "view on site" button which you may have seen. Commented Jan 15, 2021 at 9:57

3 Answers 3

8

Here is my answer. First option is to change them on Django Admin site. Second option is to create a migration file manually and run your migrations. Let me describe the second option here. Firstly, create an empty migration using following command.

$ python manage.py makemigrations --empty --name update_site_name app_name 

Next, refer to this blog to edit the migration file.

Finally, run your migrations.

Thank you.

Sign up to request clarification or add additional context in comments.

Comments

0

Finally, I changed the site_name directly in my database. I realized that a site can have a migration to define a default site_name from scratch, but for a case like mine in which there are a lot of data it's not necessary to add a migration. In other words, you should add a migration when you are developing your site, not when it's on the production stage.

Thanks to @markwalker for his comment

Comments

0

You can also change the site name from the shell if you prefer working with the shell, run python manage.py shell, get the existing site object(s) and change the names.

>>> from django.contrib.sites.models import Site >>> my_site = Site.objects.get(name='example.com') >>> my_site.name = 'mysite.com' >>> my_site.domain = 'mysite.com' >>> my_site.save() 

or you can create a new site object having your custom name. If you're creating a new site object, make sure the SITE_ID in settings.py corresponds to its id:

>>> custom_site = Site.objects.create(name='mysite.com', domain='mysite.com') >>> custom_site.save() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.