django-colorful
django-colorful copied to clipboard
Showing the color in the list
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.
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
.
Hi Simon, I used this snippet and worked perfectly for me.
Thank you very much.
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
You should probably be using django.utils.html.format_html
to format the string.
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'