@@ -57,6 +57,10 @@ def __init__(self, *args, **kwargs):
57
57
"you can set only one of the two for a ColorField instance."
58
58
)
59
59
60
+ # change choices to lower case (case-insensitive workaround)
61
+ if self .choices :
62
+ self .choices = [(k .lower (), * v ) for (k , * v ) in self .choices ]
63
+
60
64
def formfield (self , ** kwargs ):
61
65
palette = []
62
66
if self .choices :
@@ -67,11 +71,10 @@ def formfield(self, **kwargs):
67
71
kwargs ["widget" ] = ColorWidget (
68
72
attrs = {
69
73
"default" : self .get_default (),
70
- "format" : self .format ,
71
- "palette" : palette ,
72
- # # TODO: in case choices is defined,
73
- # # this will be used to hide the widget color spectrum
74
- # 'palette_choices_only': bool(self.choices),
74
+ "format" : self .format [0 :3 ] if self .format else "hex" ,
75
+ "alpha" : self .format [- 1 ] == "a" if self .format else False ,
76
+ "swatches" : palette ,
77
+ "swatches_only" : bool (self .choices ),
75
78
}
76
79
)
77
80
return super ().formfield (** kwargs )
@@ -89,6 +92,12 @@ def deconstruct(self):
89
92
kwargs ["image_field" ] = self .image_field
90
93
return name , path , args , kwargs
91
94
95
+ def validate (self , value , * args , ** kwargs ):
96
+ """
97
+ Override validation logic to make it case-insensitive.
98
+ """
99
+ super ().validate (value .lower () if value else value , * args , ** kwargs )
100
+
92
101
def _get_image_field_color (self , instance ):
93
102
color = ""
94
103
image_file = getattr (instance , self .image_field )
0 commit comments