14๐
The Django docs recommend connecting the post_migrate
signal in your app configโs ready method. The Google groups post you link to is out of date, from before the docs were updated.
You also need to specify the app config in your INSTALLED_APPS
setting.
INSTALLED_APPS = [
'myapp.apps.MyAppConfig',
# ...
]
Another way to configure your app is to use default_app_config
in __init__.py
of your app. See Configuring Applications. But the other way (dotted path to AppConfig) is preferred.
8๐
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)
- Most elegant approach for writing JSON data to a relational database using Django Models?
- Django: How to check if something is an email without a form
- Clone an inherited django model instance
- In django, is there a way to directly annotate a query with a related object in single query?
2๐
Signal post_migrate is different from other signals.
โ./manage.pyโ command will not execute the code from the apps.py files or the signals.py files
To execute this signal, place it in the models.py file.
Then you will get the desired result
- Upgrading to Django 1.7. Getting error: Cannot serialize: <storages.backends.s3boto.S3BotoStorage object
- PUT request for image upload not working in django rest
- How to override Django admin's views?
- How can I pass kwargs in URL in django