- Author:
- ikeikeikeike
- Posted:
- July 22, 2012
- Language:
- Python
- Version:
- 1.4
- Score:
- 1 (after 1 ratings)
for models.ForeignKey
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | """
Example
========
::
class InboxMessageAdmin(admin.ModelAdmin):
list_display = (
'__unicode__', "_link_inbox", "_link_message", 'read', 'read_date', "ctime", "utime", )
list_filter = ("ctime", "utime", "read", )
list_editable = ("read",)
date_hierarchy = 'ctime'
list_per_page = 50
link_generator(InboxMessageAdmin, "_link_inbox", ["inbox"])
link_generator(InboxMessageAdmin, "_link_message", ["message"])
"""
from django.core.urlresolvers import reverse
def link_generator(klass, method_name, attr):
""" link generator """
def method(self, obj):
app_name = obj.__module__.split(".")[0]
rel_obj = obj
for a in attr:
rel_obj = getattr(obj, a)
model_name = rel_obj.__class__.__name__.lower()
primary_id = rel_obj.id
url = reverse('admin:{0}_{1}_changelist'.format(app_name, model_name))
try:
return u"<a href='{0}?id__exact={1}'/>{2}.{3}</a>".format(
url, primary_id, app_name, model_name)
except KeyError:
return
method.allow_tags = True
method.short_description = method_name
setattr(klass, method_name, method)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.