8

I am using post_save signal to trigger the creation of profile data for User instance. The problem is if I create a User object from data migration then the post_save signal isn't triggering.

However, If I create User instance via shell or UserCreationForm, everything seems to be working as normal.

Cody
  • 2,480
  • 5
  • 31
  • 62
  • 6
    As far as I know, the signals are not active during migration. In fact most of Django magic is not active I think. Since if you migrate, then you can say that the model in the database is not "updated". That is in fact what the migration is all about. – Willem Van Onsem May 13 '18 at 18:14
  • Thanks for the quick reply. so what are my options? other than manually creating profile data. – Cody May 13 '18 at 18:18
  • well a signal is a function, so perhaps you can call the signal manually if you are sure that this will work. But note that this can be unsafe, since your database is between two migrations, and perhaps the signals require a model at one of the migration steps. – Willem Van Onsem May 13 '18 at 18:23
  • 3
    You have to create it manually because you cannot simply use `from myapp.models import Profile`. You must use `apps.get_model('myapp', 'Profile')`. So you cannot call your function from the migration as well. The reason is simple - if you add some fields to Profile later, and then deploy all this to some new server, you will try to insert values for that fields to the database that does not have them yet. The migrations system are using 'fake', historical versions of your models that represents the state of the model at the time the migration was created. – Alexandr Tatarinov May 14 '18 at 20:11

0 Answers0