charettes / django-colorful

Extension to the Django web framework that provides database and form color fields
https://pypi.python.org/pypi/django-colorful
MIT License
169 stars 58 forks source link

The field should support NULL values #7

Closed mehdi-behrooz closed 11 years ago

mehdi-behrooz commented 11 years ago

Sometimes it does make sense to let user choose nothing instead of a color; However I didn't find any way to feed a null value to the field.

charettes commented 11 years ago

It is considered bad practice to store NULL values in a CharField since you might end up with two values representing emptyness: NULL and '' (An empty string).

The RGBColorField is really just a CharField subclass with specific data validators thus if you want to let your user choose nothing instead of a color you should pass the blank option to your field:

from django.db import models
from colorful.fields import RGBColorField

class MyModel(models.Model):
    non_required_color_field = RGBColorField(blank=True)

If you really want to store empty values as NULL (you shouldn't :) pass along the null option to your field.

mehdi-behrooz commented 11 years ago

Charetters,

I have already passed blank=True to the RGBColorField. The problem is that once a value has been set on the field, user cannot clear the field.

--Mehdi

charettes commented 11 years ago

Sorry I misunderstood you here. Unfortunately It looks like it's a limitation of the color picker UI :/ Do you know an alternative widget that allows clearing?

mehdi-behrooz commented 11 years ago

I am not sure if you like them:

http://bgrins.github.io/spectrum/ http://belelros.github.io/jQuery-ColorPicker/

I finally ended up using this Django plugin which uses the later: https://github.com/gsiegman/django-paintstore

I guess most of people don't required this feature of inputing null values, so, thank you for your great project.

Regards, --Mehdi

charettes commented 11 years ago

I'd be open to accept a PR that replaces the actual widget with another one that supports inputing empty values.

I think both spectrum and ColorPicker could work by displaying the UI on field focus instead of hiding it thus preventing direct input.