Django Color Field
django-colorfield is a simple color field for Django models, providing a user-friendly color-picker widget in the Django admin interface. It supports various color formats including HEX, HEXA, RGB, and RGBA. The library is actively maintained, with version 0.14.0 recently released, and typically sees updates for new Django and Python versions.
Warnings
- breaking Version 0.14.0 replaced the underlying JavaScript color picker library from `jscolor` to `coloris`. If you had custom JavaScript or styling that directly interacted with `jscolor`, this will be a breaking change and require updates.
- breaking Support for older Python and Django versions has been progressively dropped. Version 0.8.0 dropped Python < 3.8 and Django < 2.2. Version 0.9.0 dropped Django 2.2. Later versions (e.g., 0.13.0) added support for Django 5.2, indicating continued forward compatibility but no backward compatibility with very old versions.
- gotcha The `max_length` of the `ColorField` was increased from 18 to 25 in version 0.10.1 to properly accommodate new formats like RGBA. If you relied on the previous `max_length` for database schema or custom validation, this change might require a database migration or adjustment of your validation rules.
Install
-
pip install django-colorfield
Imports
- ColorField
from colorfield.fields import ColorField
- ColorField
from colorfield.forms import ColorField
- ColorWidget
from colorfield.widgets import ColorWidget
Quickstart
import os
from django.db import models
from colorfield.fields import ColorField
# settings.py modification:
# INSTALLED_APPS = [
# ...,
# 'colorfield',
# ...,
# ]
# After adding 'colorfield' to INSTALLED_APPS, run 'python manage.py collectstatic'.
class Product(models.Model):
name = models.CharField(max_length=100)
# Default format is HEX
main_color = ColorField(default='#FFFFFF')
# Example with RGBA format
background_color = ColorField(format="hexa", default='#000000FF')
def __str__(self):
return f"{self.name} ({self.main_color})"