Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign keys in django admin list display

If a django model contains a foreign key field, and if that field is shown in list mode, then it shows up as text, instead of displaying a link to the foreign object.

Is it possible to automatically display all foreign keys as links instead of flat text?

(of course it is possible to do that on a field by field basis, but is there a general method?)

Example:

class Author(models.Model):     ...  class Post(models.Model):     author = models.ForeignKey(Author) 

Now I choose a ModelAdmin such that the author shows up in list mode:

class PostAdmin(admin.ModelAdmin):     list_display = [..., 'author',...] 

Now in list mode, the author field will just use the __unicode__ method of the Author class to display the author. On the top of that I would like a link pointing to the url of the corresponding author in the admin site. Is that possible?

Manual method:

For the sake of completeness, I add the manual method. It would be to add a method author_link in the PostAdmin class:

def author_link(self, item):     return '<a href="../some/path/%d">%s</a>' % (item.id, unicode(item)) author_link.allow_tags = True 

That will work for that particular field but that is not what I want. I want a general method to achieve the same effect. (One of the problems is how to figure out automatically the path to an object in the django admin site.)

like image 281
Olivier Verdier Avatar asked Mar 18 '10 13:03

Olivier Verdier


People also ask

What is admin ModelAdmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


1 Answers

I was looking for a solution to the same problem and ran across this question... ended up solving it myself. The OP might not be interested anymore but this could still be useful to someone.

from functools import partial from django.forms import MediaDefiningClass  class ModelAdminWithForeignKeyLinksMetaclass(MediaDefiningClass):      def __getattr__(cls, name):          def foreign_key_link(instance, field):             target = getattr(instance, field)             return u'<a href="../../%s/%s/%d">%s</a>' % (                 target._meta.app_label, target._meta.module_name, target.id, unicode(target))          if name[:8] == 'link_to_':             method = partial(foreign_key_link, field=name[8:])             method.__name__ = name[8:]             method.allow_tags = True             setattr(cls, name, method)             return getattr(cls, name)         raise AttributeError  class Book(models.Model):     title = models.CharField()     author = models.ForeignKey(Author)  class BookAdmin(admin.ModelAdmin):     __metaclass__ = ModelAdminWithForeignKeyLinksMetaclass      list_display = ('title', 'link_to_author') 

Replace 'partial' with Django's 'curry' if not using python >= 2.5.

like image 121
Itai Tavor Avatar answered Sep 23 '22 18:09

Itai Tavor