Getting the admin URL of a model instance in Django
If your Django project makes heavy use of the built-in Django admin, you've likely encountered a situation where you need to render a link to the Django change form for a particular model instance. For example, I currently have a project that uses the Django admin as a back-office tool where email notifications are sent to site administrators whenever a new entry is created via a user form entry. This is fairly easy to do by tacking on a small function to your model object:
class MyModel(models.Model):
some_field = models.CharField()
def get_admin_url(self):
# the url to the Django admin form for the model instance
info = (self._meta.app_label, self._meta.model_name)
return reverse('admin:%s_%s_change' % info, args=(self.pk,))
Reference: https://stackoverflow.com/a/10420333