pythondjangodjango-signalsdjango-simple-history

How to specify a generated model class, from django-simple-history, as a sender for a signal?


I would like to listen for pre_create_historical_record signal provided by simple-history package but only from certain sender. The problem is that historical models are generated by simple-history and I can't figure out how can I import class "core.HistoricalUser" as a type to set as sender.


Solution

  • You can get to the model class by going through the history manager as documented here.

    In your specific case something like – assuming your history manager is called history and the model you are tracking history for is User:

    pre_create_historical_record.connect(
        signal_receiver_function, 
        sender=User.history.model
    )