Django Sites Framework: Initial Data Migration Location

后端 未结 3 1609
不思量自难忘°
不思量自难忘° 2020-12-16 14:06

Before Django 1.7, when using the Django Sites Framework one could/should define the initial data using Initial Fixtures.

myproject/fixtures/initial_

3条回答
  •  遥遥无期
    2020-12-16 14:24

    A couple of tweaks to @Jason's answer. Last tested with Django 2.2.

    from django.conf import settings
    from django.db import migrations
    
    
    def update_site_domain(apps, schema_editor):
        Site = apps.get_model("sites", "Site")
        s, _ = Site.objects.get_or_create(pk=settings.SITE_ID)
        s.domain = settings.HOST
        s.save()
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ("your_app", "last_migration_name"),
            ("sites", "0002_alter_domain_unique"),  # highest-numbered migration for the sites framework
        ]
    
        operations = [migrations.RunPython(update_site_domain)]
    

提交回复
热议问题