Created
December 13, 2012 00:14
-
-
Save timmyomahony/4272926 to your computer and use it in GitHub Desktop.
Automatically generating admin URLs for your objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Genius!! How many times I've bulit in a get_admin_url method... Why provide @permalink and reverse separately?