receiving django post_migrate signal

前端 未结 3 1169
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 09:57

I\'m doing some kind of refactoring for my project, where I\'m relying on the django django.contrib.auth.models.Permission model. So far I define the permission

3条回答
  •  囚心锁ツ
    2020-12-15 10:43

    I have had done a post_migrate example for another question before. I will write down its solution. Maybe it will be helpful for you.

    # in apps.py
    
    ...
    from django.conf import settings
    from django.db.models.signals import post_migrate
    
    def create_default_site_profile(sender, **kwargs):
        """after migrations"""
        from django.contrib.sites.models import Site
        from core.models import SiteProfile
    
        site = Site.objects.get(id=getattr(settings, 'SITE_ID', 1))
    
        if not SiteProfile.objects.exists():
            SiteProfile.objects.create(site=site)
    
    class CoreConfig(AppConfig):
        name = 'core'
    
        def ready(self):
            post_migrate.connect(create_default_site_profile, sender=self)
            
            # if you have other signals e.g. post_save, you can include it 
            # like the one below.
            from .signals import (create_site_profile)  
    

提交回复
热议问题