Skip to content

Instantly share code, notes, and snippets.

@timmyomahony
Created December 13, 2012 00:14
Show Gist options
  • Save timmyomahony/4272926 to your computer and use it in GitHub Desktop.
Save timmyomahony/4272926 to your computer and use it in GitHub Desktop.
Automatically generating admin URLs for your objects
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.db import models
class AdminURLMixin(object):
def get_admin_url(self):
content_type = ContentType \
.objects \
.get_for_model(self.__class__)
return reverse("admin:%s_%s_change" % (
content_type.app_label,
content_type.model),
args=(self.id,))
@models.permalink
def alternative_admin_url(self):
content_type = ContentType \
.objects \
.get_for_model(self.__class__)
return ('admin:%s_%s_change' % (
content_type.app_label,
content_type.model), [str(self.id)])
@yuchant
Copy link

yuchant commented Dec 14, 2012

Genius!! How many times I've bulit in a get_admin_url method... Why provide @permalink and reverse separately?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment