djangomigrationsitesdjango-sites

Migration file don't change Site name and domain


I have this migration file:

from django.db import migrations
from django.contrib.sites.models import Site as DjangoSite


def change_django_site(apps, schema_editor):
    site = DjangoSite.objects.get(id=1)
    site.delete()

    DjangoSite.objects.get_or_create(id=1, domain='www.xprende.com', name='XPrende')


def create_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    Page = apps.get_model('wagtailcore.Page')
    Site = apps.get_model('wagtailcore.Site')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    # If migration is run multiple times, it may have already been deleted
    Page.objects.filter(id=2).delete()

    # Create content type for homepage model
    homepage_content_type, __ = ContentType.objects.get_or_create(
        model='homepage', app_label='home')

    # Create a new homepage
    homepage = HomePage.objects.create(
        title="Home",
        draft_title="Home",
        slug='home',
        content_type=homepage_content_type,
        path='00010001',
        depth=2,
        numchild=0,
        url_path='/home/',
    )

    # Create a site with the new homepage set as the root
    Site.objects.create(
        hostname='www.xprende.com', site_name='XPrende', root_page=homepage, is_default_site=True
    )


def remove_homepage(apps, schema_editor):
    # Get models
    ContentType = apps.get_model('contenttypes.ContentType')
    HomePage = apps.get_model('home.HomePage')

    # Delete the default homepage
    # Page and Site objects CASCADE
    HomePage.objects.filter(slug='home', depth=2).delete()

    # Delete content type for homepage model
    ContentType.objects.filter(model='homepage', app_label='home').delete()


class Migration(migrations.Migration):
    dependencies = [
        ('home', '0001_initial'),
        ('sites', '0002_alter_domain_unique'),
    ]

    operations = [
        migrations.RunPython(
            create_homepage,
            remove_homepage,
            change_django_site,
        ),
    ]

As you can see i made a function that have to change the django site name and domain. But it doesn't do it, after apply the migrations i get the same default values:

In [3]: DjangoSite.objects.get(id=1)
Out[3]: <Site: example.com>

In [4]: DjangoSite.objects.get(id=1).name
Out[4]: 'example.com'

In [5]: DjangoSite.objects.get(id=1).domain
Out[5]: 'example.com'

In [6]: DjangoSite.objects.all()
Out[6]: <QuerySet [<Site: example.com>]>

I can't understand why, i put the sites migrations as dependencie of this migration file but still it doesn't works.

Can anyone explain me why i have this error?

Thank you.


Solution

  • You have to run change_django_site as separate operation:

    class Migration(migrations.Migration):
        dependencies = [
            ('home', '0001_initial'),
            ('sites', '0002_alter_domain_unique'),
        ]
    
        operations = [
            migrations.RunPython(change_django_site, reverse_code=migrations.RunPython.noop),
            migrations.RunPython(create_homepage, reverse_code=remove_homepage),
        ]
    

    and change_django_site must be

    def change_django_site(apps, schema_editor): 
        DjangoSite = apps.get_model('sites', 'Site') 
        site = DjangoSite.objects.get_or_create(id=1)
        site.domain='www.xprende.com'
        site.name='XPrende'
        site.save()