django-colorfield Documentation

repository·main·Indexed 20 days ago

https://github.com/fabiocaccamo/django-colorfield

A Django application that adds color field support to models with a color-picker for the admin and standard forms. Supports hex, hexa, rgb, and rgba formats, allows for restrictive color choices or non-restrictive samples, and can automatically extract colors from the top-left pixel of an image field.

Tokens
1K
Snippets
7
Records
7
Agent score
21%

What's inside django-colorfield

  1. Provide color samples or restrict to choices

    main

    You can provide a palette of colors to the widget using two different approaches:

    1. samples (Non-restrictive): Displays a palette of colors for quick selection, but still allows the user to pick any other color from the spectrum.
    2. choices (Restrictive): Forces the user to choose exactly one of the colors provided in the palette.

    Example palette format:

    COLOR_PALETTE = [
        ("#FFFFFF", "white", ),
        ("#000000", "black", ),
    ]
    from colorfield.fields import ColorField
    from django.db import models
    
    class MyModel(models.Model):
        COLOR_PALETTE = [
            ("#FFFFFF", "white", ),
            ("#000000", "black", ),
        ]
    
        # Not restrictive: allows selection from spectrum
        color = ColorField(samples=COLOR_PALETTE)
    
        # Restrictive: mandatory to choose from palette
        color = ColorField(choices=COLOR_PALETTE)
  2. Install django-colorfield

    main

    To use django-colorfield in your Django project, follow these steps:

    1. Install the package via pip:
      pip install django-colorfield
    2. Add colorfield to your INSTALLED_APPS in settings.py.
    3. Run python manage.py collectstatic to ensure the color picker assets are available.
    4. Restart your application server.
    pip install django-colorfield
  3. Auto-populate color from an image

    main

    Use the image_field option to automatically extract a color from an image. The color is calculated from the top-left pixel of the specified image field every time the model instance is saved.

    from colorfield.fields import ColorField
    from django.db import models
    
    class MyModel(models.Model):
        image = models.ImageField(upload_to="images")
        color = ColorField(image_field="image")
  4. Use ColorField in plain Django Forms

    main

    For forms that are not tied to a model, use colorfield.forms.ColorField to provide a color-picker widget.

    from django import forms
    from colorfield.forms import ColorField
    
    class MyForm(forms.Form):
        color = ColorField(initial="#FF0000", format="hex")
  5. Use ColorField in ModelForms

    main

    When using colorfield.fields.ColorField within a django.forms.ModelForm, it automatically provides a color picker widget and validates the color format based on the field's format setting.

    from django import forms
    
    class MyModelForm(forms.ModelForm):
        class Meta:
            model = MyModel
            fields = ["color"]
  6. Use ColorField in Django Models

    main

    You can add color selection capabilities to your models by using colorfield.fields.ColorField. This provides a color picker in the Django admin interface.

    from colorfield.fields import ColorField
    from django.db import models
    
    class MyModel(models.Model):
        color = ColorField(default='#FF0000')
  7. Configure ColorField format

    main

    The format option determines how the color value is stored and validated. Supported formats are:

    • hex (default)
    • hexa
    • rgb
    • rgba
    from colorfield.fields import ColorField
    from django.db import models
    
    class MyModel(models.Model):
        color = ColorField(format="hexa")