Django Admin Cookbook-38 how to get a particular object Django Admin backend URL

38. How to obtain the specific object of Django Admin backend URL?

Hero model has a children field that displays each child hero's name. You will be asked to link to each childrin Hero model to change the page. To achieve the following:

@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
    ...
    def children_display(self, obj):
        display_text = ", ".join([
            "<a href={}>{}</a>".format(
                    reverse('admin:{}_{}_change'.format(obj._meta.app_label, obj._meta.model_name),
                    args=(child.pk,)),
                child.name)
             for child in obj.children.all()
        ])
        if display_text:
            return mark_safe(display_text)
        return "-"

More reverse('admin:{}_{}_change'.format(obj._meta.app_label, obj._meta.model_name), args=(child.pk,))returns an object back to modify the URL of the page.

Other pages are as follows:

  • Delete Page URL: reverse('admin:{}_{}_delete'.format(obj._meta.app_label, obj._meta.model_name), args=(child.pk,))
  • History Page URL: reverse('admin:{}_{}_history'.format(obj._meta.app_label, obj._meta.model_name), args=(child.pk,))

Back to Contents

Guess you like

Origin www.cnblogs.com/superhin/p/12192363.html