django-colorful icon indicating copy to clipboard operation
django-colorful copied to clipboard

Showing the color in the list

Open julianofischer opened this issue 11 years ago • 5 comments

Hi, I created a model with a RGBField. Then I setted my model admin list_display to: ('color') where color is my RGBField.

In the list of registered models, the rgb value for the selected color is shown. Would be amazing if the view was something like the widget.

I do not know if it can be done directly in the django-colorful but if it is possible, it will be pretty cool.

julianofischer avatar Jan 22 '14 18:01 julianofischer

I guess both a display helper and list filter could be added. While I find time to work on this you can use the following snippet.

def display_color(obj):
    return '<span style="width:5px;height:5px;color:%s"></span>' % obj.color
display_color.short_description = 'Color'
display_color.allow_tags = True

Add add it as a callable (pass the object directly) to your list_display.

charettes avatar Jan 23 '14 02:01 charettes

Hi Simon, I used this snippet and worked perfectly for me.

Thank you very much.

julianofischer avatar Jan 23 '14 13:01 julianofischer

That didn't show up for me in Grappelli admin fyi. Solved it like this (and actually I liked it better):

def display_color(self):
    return '<span style="font-weight:bold;color:{color}">{color}</span>'.format(
        color=self.color,
    )
display_color.short_description = _('Color')
display_color.allow_tags = True

kirpit avatar Mar 20 '15 08:03 kirpit

You should probably be using django.utils.html.format_html to format the string.

Proper-Job avatar Aug 12 '15 10:08 Proper-Job

This worked worked for me on Django 4.2:

from django.utils.html import format_html

[...]
    def display_color(self):
        return format_html('<span style="width:15px;height:15px;display:block;background-color:{}"></span>', self.color)
    display_color.short_description = 'Color'

onny avatar Nov 13 '23 12:11 onny