I'm trying to do something that seems relatively simple, but I'm completely stumped. I have a model, and I want to send an e-mail out once an instance is created for the first time. I can do this easily enough using this:
@receiver(models.signals.post_save, sender=MyModel)
def execute_after_save(sender, instance, created, *args, **kwargs):
if created:
send_mail(mail_type='MyModel_created', instance=instance)
However, the problem is that I need to send a link in the e-mail to the change view of the model in the admin panel. I've been using code like this (in other cases) to construct the link inside the send_mail() function mentioned above:
mymodel_admin_url = request.build_absolute_uri(reverse('admin:mymodels_mymodel_change', args=(instance.id,)))
But you'll notice that this relies on having a request object, which isn't passed through the post_save signal, nor even the save method. What's the best way to get the link I need in this case?
Rebuilding and overriding everything (i.e. the save method, the base_save method, the post_save signal, etc.) to pass requests through seems like a nightmare. However, I can't get the link I need using the sites framework either (sometimes I use the site on subdomains, or for example when using manage.py runserver for testing, and calling on the sites framework doesn't build the correct links).