koddr
koddr

Reputation: 784

Django Admin. How to add background color for each row in list view, when object has boolean field == True?

My env is: Django 2.0.6, Python 3.6.4

I have standard Django Admin (with inline edit):

standard Django Admin

How to add background color for each row in list view, when object has field is_active_city=True? For example, background-color: green; for is_active_city=True, like this:

background-color: green

Upvotes: 2

Views: 2162

Answers (1)

Bijoy
Bijoy

Reputation: 1131

There's a package django-liststyle which may satisfy your need

Install it using pip install django-liststyle==0.2b

then in settings.py add 'liststyle' to INSTALLED_APPS list.

so now the admin.py for the relevant model would be

from liststyle import ListStyleAdminMixin

class CityAdmin(admin.ModelAdmin, ListStyleAdminMixin):
    ...
    def get_row_css(self, obj, index):
        if obj.is_active_city:
            return 'green'
        return 'red'  # or any color for False

NOTE: If there are issues regarding future package error, refer this link

Upvotes: 2

Related Questions