Opened 7 years ago

Closed 23 months ago

#8261 closed New feature (fixed)

ModelAdmin hook for customising the "show on site" button

Reported by: jarrow Owned by: unaizalakain
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: almir@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

As Simon Willison suggested on django developers:

Further thinking about get_absolute_url. At the moment, the main 
reason it's a standard in Django is that it is used by 
django.contrib.admin to display the "show on site" button inside the 
admin interface. 

This mechanism isn't very robust. Admin sites are often run on 
different subdomains from the rest of the site, which means that 
get_absolute_url on its own isn't enough - instead, we have to go 
through the whole /r/{{ content-type-id }}/{{ object-id }}/ palaver 
which attempts to figure out the site based on ForeignKey(Site) 
properties on the model, or by using the default site from SITE_ID. 

It would make a lot more sense to allow ModelAdmin subclasses to 
define that behaviour themselves, for example: 

class ArticleAdmin(admin.ModelAdmin): 
    ... 
    def show_on_site(self, obj): 
        return 'http://livesite.example.com' + obj.get_url_path() 

We could even support "show_on_site = True" as meaning "include the 
show on site link, using cleverness to figure out the link" whereas 
show_on_site = a_callable means "call this function to figure out the 
URL for that link".

Attachments (2)

show_on_site.diff (1.8 KB) - added by redduck666 7 years ago.
view_on_site.diff (16.9 KB) - added by kratorius 7 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 Changed 7 years ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Changed 7 years ago by redduck666

comment:2 Changed 7 years ago by redduck666

  • Cc almir@… added
  • Has patch set

i wonder, am i on the right track?

comment:3 Changed 7 years ago by kratorius

  • Owner changed from nobody to kratorius

Attached patch implements view_on_site as either a boolean flag or a callable. There are two issues I had to deal with:

  • I made default for view_on_site False since we're not relying on get_absolute_url anymore, so there's no way to effectively know whether we have a URL where we can go or not;
  • I had to add the view_on_site_url parameter to InlineAdminForm since there was no way to access to the InlineModelAdmin object from there. This could break backward compatibility if someone was inheriting from InlineModelForm (that's not documented anywhere though);

Apart from this, patch includes tests and documentation.

Changed 7 years ago by kratorius

comment:4 Changed 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:5 Changed 4 years ago by patchhammer

  • Easy pickings unset
  • Patch needs improvement set

view_on_site.diff fails to apply cleanly on to trunk

comment:6 Changed 4 years ago by julien

  • UI/UX set

comment:7 Changed 23 months ago by unaizalakain

  • Owner changed from kratorius to unaizalakain
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:8 Changed 23 months ago by charettes

  • Patch needs improvement set

Added some comments on the PR.

comment:9 Changed 23 months ago by unaizalakain

  • Patch needs improvement unset

Proposed changes applied!

comment:10 Changed 23 months ago by charettes

  • Patch needs improvement set

Finished my review, sorry for the delayed comments.

comment:11 Changed 23 months ago by unaizalakain

  • Patch needs improvement unset

It doesn't matter, PR updated!

comment:12 Changed 23 months ago by charettes

  • Triage Stage changed from Accepted to Ready for checkin

Patch looks RFC, please rebase and I'll merge it.

comment:13 Changed 23 months ago by unaizalakain

Tim Grahams suggestions taken into account and rebased master on it!

comment:14 Changed 23 months ago by unaizalakain

Once again!

comment:15 Changed 23 months ago by timo

This looks good to me as well.

comment:16 Changed 23 months ago by Simon Charette <charette.s@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In fd219fa24c7911adab60e1f5e4fd3d7f9d82a969:

Fixed #8261 -- ModelAdmin hook for customising the "show on site" button

ModelAdmin.view_on_site defines wether to show a link to the object on the
admin detail page. If True, cleverness (i.e. Model.get_absolute_url) is
used to get the url. If it's a callable, the callable is called with the object
as the only parameter. If False, not link is displayed.

With the aim of maitaining backwards compatibility, True is the default.

Note: See TracTickets for help on using tickets.
Back to Top