A ModelAdmin hook for customising the "show on site" button?

2 views
Skip to first unread message

Simon Willison

unread,
Aug 12, 2008, 8:02:56 AM8/12/08
to 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".

Thoughts?

Brian Rosner

unread,
Aug 12, 2008, 9:53:04 AM8/12/08
to django-d...@googlegroups.com

It sounds reasonable to me. I don't see anything wrong the solution. I
suppose it could be implemented regardless of the get_absolute_url
bike-shedding/change. It would also be a but important to do this on
inlines as well. It could like fix #7984 [1] in the process Timing is
a bit critical as we are reaching the beta in a couple of days. I
don't think adding this post-beta is a good idea or even allowed ;)

[1]: http://code.djangoproject.com/ticket/7984

--
Brian Rosner
http://oebfare.com

Jonas Pfeil

unread,
Aug 12, 2008, 10:50:47 AM8/12/08
to Django developers
> 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".

+1 on that.

This has also bitten me. Would be really cool if it gets in!

Jonas

Eric Holscher

unread,
Aug 12, 2008, 10:51:06 AM8/12/08
to Django developers
This also sounds like a change that could be added post-1.0 and not
break anything. So adding it after the 1.0 release won't be backwards
incompatible, and a pretty easy change.

Jonas Pfeil

unread,
Aug 12, 2008, 10:55:43 AM8/12/08
to Django developers
> 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".

Brian Rosner

unread,
Aug 12, 2008, 11:01:52 AM8/12/08
to django-d...@googlegroups.com
On Tue, Aug 12, 2008 at 8:51 AM, Eric Holscher <eric.h...@gmail.com> wrote:
>
> This also sounds like a change that could be added post-1.0 and not
> break anything. So adding it after the 1.0 release won't be backwards
> incompatible, and a pretty easy change.

It certainly could be. However, if Simon wants to spend the time to
get it in before the beta he is more than welcome to :)

Reply all
Reply to author
Forward
0 new messages