Add this to your model to be able to get their admin change link from anywhere
Useful if you want to jump to the admin screen of an object you are looking at on the front end
1 2 3 4 5 6 | from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
def get_admin_url(self):
content_type = ContentType.objects.get_for_model(self.__class__)
return urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(self.id,))
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 4 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Nice. Minor tweak, args=(self.pk,) instead, to support models with explicit primary_key.
#
You can do it without using a query to ContentTypes!
#
@twoolie Your solution doesn't work for me, only the original solution works. Because in my model, self._meta.module_name doesn't exist.
#
self._meta.module_name should be self._meta.model_name.
#
Please login first before commenting.