I wanted to update the site domain/name using data migrations in Django 1.7:
$ python manage.py makemigrations main
Migrations for 'main':
0001_currencies_locations_posts_userprofile.py:
- Create model Currencies
- Create model Locations
- Create model Posts
- Create model UserProfile
$
$ python manage.py syncdb
Operations to perform:
Synchronize unmigrated apps: allauth, account, socialaccount
Apply all migrations: sessions, admin, auth, sites, contenttypes, main
Synchronizing apps without migrations:
Creating tables...
Creating table account_emailaddress
Creating table account_emailconfirmation
Creating table socialaccount_socialapp_sites
Creating table socialaccount_socialapp
Creating table socialaccount_socialaccount
Creating table socialaccount_socialtoken
Installing custom SQL...
Installing indexes...
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying main.update_site...Traceback (most recent call last):
File "/Users/ajangity/Dev/lib/python3.4/site-packages/django/contrib/sites/models.py", line 52, in get_current
current_site = SITE_CACHE[sid]
KeyError: 1
…
sqlite3.OperationalError: no such table: django_site
I have a main/migration/update_site.py:
def update_site(apps, schema_editor):
current_site = Site.objects.get_current()
current_site.domain = “
example.com"
current_site.name = “Example"
current_site.save()
class Migration(migrations.Migration):
operations = [
migrations.RunPython(update_site),
]
Doing a syncdb throws this error. It’s probably because the “Site” hasn’t been created by the time I am trying to update it.
So, the question is, how do I programmatically update the Site info?
Thanks