Django Sites Framework: Initial Data Migration Location

后端 未结 3 1593
不思量自难忘°
不思量自难忘° 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:22

    You just need to reference the highest-numbered sites migration as a dependency.

    def forward(apps, schema_editor):
        Site = apps.get_model("sites", "Site")
        db_alias = schema_editor.connection.alias
        s, created = Site.objects.using(db_alias).get_or_create(pk=1)
        s.name = APP_NAME
        s.domain = APP_NAME
        s.save()
    
    
    def reverse(apps, schema_editor):
        Site = apps.get_model("sites", "Site")
        db_alias = schema_editor.connection.alias
        s = Site.objects.using(db_alias).get(pk=1)
        s.name = ORIG_APP_NAME
        s.domain = ORIG_APP_NAME
        s.save()
    
    
    class Migration(migrations.Migration):
    
        dependencies = [
    
            # `core` is the app containing this migration
            ('core', '0001_initial'),
    
            # `0002_alter_domain_unique` is the highest-numbered migration for
            # the sites framework
            ('sites', '0002_alter_domain_unique'),
    
        ]
    
        operations = [
            migrations.RunPython(forward, reverse)
        ]
    

    This was tested on Django 1.11.2.

    Fwiw, the MODULE_MIGRATIONS solution above does not work for me.

提交回复
热议问题