Search code examples
djangodjango-simple-history

Generate url for Django Simple History historical object


Given an model called Stuff, I want the url to a HistoricalStuff object.

In other words, how does one implement get_historical_url in the below code snippet?

stuff = Stuff.objects.first()
stuff.pk
-> 100
historical_stuff = stuff.history.first()  # we want to get url for this
historical_stuff.pk
-> 1
get_historical_url(historical_stuff)
-> /path/to/admin/stuff/100/history/1

Obviously the dumb solution would be to use a format string but I'd rather use urlresolvers


Solution

  • After much digging around, I found in the simple history source code that the url name is similar to the admin change names, namely admin_%s_%s_simple_history.

    With this knowledge, get_historical_url looks like

    def get_simplehistory_url(history_obj):
        parent_obj = history_obj.history_object
        return urlresolvers.reverse('admin:{}_{}_simple_history'.format(
            parent_obj._meta.app_label, parent_obj._meta.model_name), args=(parent_obj.id, history_obj.pk))