1

What I want to do is that I want to change Site display name and domain from example.com to mydomain.com . Normally, I can enter into the django admin and do this. But I want to use data migration. My code is as below:

from django.db import migrations


def change_site_name_from_example_to_mydomain_func(apps, schema_editor):
    Site = apps.get_model('sites', 'Site')
    site = Site.objects.get(name='example.com')
    site.name = 'mydomain.com'
    site.domain = 'mydomain.com'
    site.save()


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0006_populate_database_createsuperuser'),
    ]

    operations = [
        migrations.RunPython(change_site_name_from_example_to_mydomain_func),
    ]

However, I get an error saying that there is no such app as sites. Te question is, how can I use Site model in a data migration? The error is this: LookupError: No installed app with label 'sites'.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • In what app did you define `Site`, at first sight it looks like it is defined in `'accounts'`, not `'sites'`? – Willem Van Onsem Sep 21 '19 at 11:22
  • This app is predefined by django – Amin Ba Sep 21 '19 at 11:26
  • You can import it by ```from django.contrib.sites.models import Site``` – Amin Ba Sep 21 '19 at 11:27
  • are you sure `django.contrib.sites` is in the list of `INSTALLED_APPS`? – aliva Sep 21 '19 at 11:28
  • However, in data migration, I should get an app by `apps.get_app('app', 'model_name)` and I do not know how to do it for Site – Amin Ba Sep 21 '19 at 11:28
  • I have this in settings: ` ```django.contrib.sites', # new``` – Amin Ba Sep 21 '19 at 11:29
  • I found this https://code.djangoproject.com/ticket/22482 but I do not yet have a solution on how to change Site object in data migration – Amin Ba Sep 21 '19 at 11:46
  • This may as well be the answer but I am still unable to do it. https://stackoverflow.com/questions/29565665/no-installed-app-with-label-admin-running-django-migration-the-app-is-insta – Amin Ba Sep 21 '19 at 12:00
  • I did not try anything special. I tried using ```django.contrib.sites``` instead of app name. I provided the link to related topics so that these can give a hint to the reader of the question. I did not understand what I have to do @Alasdair – Amin Ba Sep 21 '19 at 14:24

2 Answers2

3

The question you linked to suggest that you add a dependency to the sites app, e.g.

dependencies = [
    ('accounts', '0006_populate_database_createsuperuser'),
    ('sites', '0002_alter_domain_unique'),
]
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I got the same error: ```LookupError: No installed app with label 'django.contrib.sites'.``` – Amin Ba Sep 22 '19 at 04:32
  • Make sure you have `Site = apps.get_model('sites', 'Site')` like in your original question. If it still doesn’t work, then update your question with the new code and full traceback. – Alasdair Sep 22 '19 at 08:50
  • Can you please tell me where can I see this migration file??!! ```0002_alter_domain_unique``` @Alasdair – Amin Ba Sep 22 '19 at 08:59
  • It’s in the `django.contrib.sites` app. The contents of that migration don’t matter - we’re using it because it’s the last migration for the sites app (in the current version of Django). – Alasdair Sep 22 '19 at 10:14
  • By the way, it did not solve the problem. It seems as if it is a problem of the django, based on the ticket – Amin Ba Sep 23 '19 at 09:46
  • The ticket is closed as fixed, which suggests it isn’t a problem with Django. I’m afraid I haven’t been able to test my answer, so I can’t confirm if the problem is in my answer, your code, or Django. – Alasdair Sep 23 '19 at 10:15
  • is ```0002_alter_domain_unique``` the same in every project or should I use something else. I used exactly ```0002_alter_domain_unique``` – Amin Ba Sep 23 '19 at 10:23
  • I used `0002_alter_domain_unique` because it's [the last migration in the sites app](https://github.com/django/django/tree/f97bbad908df128189eff77d98af9a25ed1ecf23/django/contrib/sites/migrations) in the current version of Django. You should use it as well. If a future version of Django adds `0003_xxx` then you might want to use that instead. – Alasdair Sep 23 '19 at 11:11
  • I checked mine and it was ```0002_alter_domain_unique``` . Ok, I need to do all again to find out what is happening and what is the problem. – Amin Ba Sep 23 '19 at 11:15
1

Have you tried python manage.py migrate sites? And then try again python manage.py migrate

Thomas
  • 1,956
  • 14
  • 21