Django Color Field

0.14.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

To quickly get started, install the package, add 'colorfield' to your `settings.INSTALLED_APPS`, and then define `ColorField`s in your Django models. After modifying `INSTALLED_APPS`, remember to run `python manage.py collectstatic` for the color picker to appear correctly in the admin. The `ColorField` automatically provides a color picker in the Django admin interface.

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})"

view raw JSON →