Django post_save signal not triggering from data migration
Asked Answered
G

0

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.

Gaitskell answered 13/5, 2018 at 18:13 Comment(4)
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.Hardaway
Thanks for the quick reply. so what are my options? other than manually creating profile data.Gaitskell
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.Hardaway
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.Viceregal

© 2022 - 2024 — McMap. All rights reserved.