Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customising Django admin TabularInline default field

I have a TabularInline admin layout, all works fine except I'd like to have it show something other than the Obj.__unicode__ value on the top left of each row.

My TabularInline is a photologue ImageModel model, so I'd like it to show me the thumbnail instead of the regular __unicode__ result.

I tried to change __unicode__ to output the thumbnail, which works, except the HTML is escaped so I get <img src="XXX"...... etc

Is there an easy way to mark my __unicode__ method as a safe string? Or a way to override the property the admin chooses to display?

I've tried this:

__unicode__.is_safe = True 

But that doesn't work.

like image 651
Guy Bowden Avatar asked Feb 09 '10 09:02

Guy Bowden


People also ask

Can I customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

What is list_ display in Django?

It provides a simple UI for creating, editing and deleting data defined with the Django ORM. In this article we are going to enable the admin user interface for a simple model and customize it from a simple list view to a more user friendly table like interface.

Where is Django admin template?

To view the default admin template you can access it in the django/contrib/admin/templates/admin folder. In this situation you will most likely be using a virtual environment and can find this folder in the directory that contains all the installed libraries.


1 Answers

You can customize the template for you TabularInline to make it look the way you want. I think it's a better idea then hacking __unicode__:

class PhotoInline(admin.TabularInline):
    model = Photo
    template = 'photologue/photoinline.html'

The easiest way to create your is to copy and customize the default django/contrib/admin/templates/admin/edit_inline/tabular.html template.

like image 76
Ludwik Trammer Avatar answered Oct 07 '22 16:10

Ludwik Trammer