updating site domain with data migration

118 views
Skip to first unread message

Anil Jangity

unread,
Sep 21, 2014, 8:30:55 PM9/21/14
to django...@googlegroups.com
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



Markus Holtermann

unread,
Sep 21, 2014, 10:34:39 PM9/21/14
to django...@googlegroups.com
Hey Anil,

On Sun, Sep 21, 2014 at 05:29:18PM -0700, Anil Jangity wrote:
>$ python manage.py syncdb

Just as a side note: "syncdb" is deprecated and is being replaced by
"migrate".

>
>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),
> ]


You are missing a dependency to the sites app in your migration as well
as to the previous migration in the "main" app. Have a look at
https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies

Adding

dependencies = [
("main", "whatever_the_name_of_the_previous_migration_is"),
("sites", "0001_initial"),
]

should work.

I tried to explain the way dependencies in Django's new migration
framework work in a blog post:
https://markusholtermann.eu/2014/09/django-17-database-migrations-done-right/#how-do-dependencies-between-migrations-work
Might be interesting.

/Markus

Anil Jangity

unread,
Sep 22, 2014, 11:42:28 AM9/22/14
to django...@googlegroups.com
Thanks, looks like I need to do a second pass at the documentation. ;-)

In my dev environment, I was trying to do a clean/fresh install; including the creation of the super user account.
If I don’t use syncdb, I should also create the super user account using data migration? If so, it seems like I need to go do User.objects.create_superuser().

Thanks!

Markus Holtermann

unread,
Sep 22, 2014, 12:18:01 PM9/22/14
to django...@googlegroups.com
On Mon, Sep 22, 2014 at 08:41:36AM -0700, Anil Jangity wrote:
>If I don't use syncdb, I should also create the super user account
>using data migration? If so, it seems like I need to go do
>User.objects.create_superuser().

There still is the option of running "python manage.py createsuperuser"
after the migrations are applied. If I don't need a superuser account
during the migration, this would be my way of doing it.

/Markus

Anil Jangity

unread,
Sep 23, 2014, 12:08:35 PM9/23/14
to django...@googlegroups.com
Markus, one specific question re your blog post.

"What happens when you call python manage.py makemigrations? First of all, since no apps are given, Django reads the migrations from all apps listed in INSTALLED_APPS. In our case, this is ('author', 'book',)

I have the following in INSTALLED_APPS:

In [2]: apps.get_app_configs()
Out[2]: ValuesView(OrderedDict([('admin', <AdminConfig: admin>), ('auth', <AuthConfig: auth>), ('contenttypes', <ContentTypesConfig: contenttypes>), ('sessions', <SessionsConfig: sessions>), ('messages', <MessagesConfig: messages>), ('staticfiles', <StaticFilesConfig: staticfiles>), ('sites', <SitesConfig: sites>), ('allauth', <AppConfig: allauth>), ('account', <AccountConfig: account>), ('socialaccount', <SocialAccountConfig: socialaccount>), ('main', <MainConfig: main>)]))

But it seems it doesn’t see my “main” app. Your note seems to indicate that it will ‘make migrations’ for INSTALLED_APPS.

$ python manage.py migrate --list
admin
 [ ] 0001_initial
auth
 [ ] 0001_initial
contenttypes
 [ ] 0001_initial
sessions
 [ ] 0001_initial
sites
 [ ] 0001_initial
socialaccount
 (no migrations)

I have to explicitly run with ‘main’ app. Just wondering if I am missing something in my config.

$ python manage.py makemigrations main
Migrations for 'main':
  0001_initial.py:
    - Create model Currencies
    - Create model Locations
    - Create model Posts
    - Create model UserProfile
Reply all
Reply to author
Forward
0 new messages