Opened 16 years ago

Closed 10 years ago

#8261 closed New feature (fixed)

ModelAdmin hook for customising the "show on site" button

Reported by: Owned by: Unai Zalakain
Component: contrib.admin Version: dev
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 15 years ago.
view_on_site.diff (16.9 KB ) - added by Ivan Giuliani 15 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 by Jacob, 16 years ago

Triage Stage: UnreviewedAccepted

by redduck666, 15 years ago

Attachment: show_on_site.diff added

comment:2 by redduck666, 15 years ago

Cc: almir@… added
Has patch: set

i wonder, am i on the right track?

comment:3 by Ivan Giuliani, 15 years ago

Owner: changed from nobody to Ivan Giuliani

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.

by Ivan Giuliani, 15 years ago

Attachment: view_on_site.diff added

comment:4 by Luke Plant, 13 years ago

Severity: Normal
Type: New feature

comment:5 by patchhammer, 13 years ago

Easy pickings: unset
Patch needs improvement: set

view_on_site.diff fails to apply cleanly on to trunk

comment:6 by Julien Phalip, 13 years ago

UI/UX: set

comment:7 by Unai Zalakain, 10 years ago

Owner: changed from Ivan Giuliani to Unai Zalakain
Patch needs improvement: unset
Status: newassigned

comment:8 by Simon Charette, 10 years ago

Patch needs improvement: set

Added some comments on the PR.

comment:9 by Unai Zalakain, 10 years ago

Patch needs improvement: unset

Proposed changes applied!

comment:10 by Simon Charette, 10 years ago

Patch needs improvement: set

Finished my review, sorry for the delayed comments.

comment:11 by Unai Zalakain, 10 years ago

Patch needs improvement: unset

It doesn't matter, PR updated!

comment:12 by Simon Charette, 10 years ago

Triage Stage: AcceptedReady for checkin

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

comment:13 by Unai Zalakain, 10 years ago

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

comment:14 by Unai Zalakain, 10 years ago

Once again!

comment:15 by Tim Graham, 10 years ago

This looks good to me as well.

comment:16 by Simon Charette <charette.s@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

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