I have a small problem I'd like to know the best way to store eyes or hairs colors with django.
This is what I did before asking myself what the best way :)
HAIR_COLORS = (
('1', 'braun'),
('2', 'blond'),
('3', 'red'))
hair_color = models.IntegerField(max_length=1, choices=HAIR_COLORS)
Is it a good choice ? Do I have to replace The integerField() by a CharField() with choices like:`
HAIR_COLORS = (
('braun', 'braun'),
('blond', 'blond'),
('red', 'red'))
In fact I'm not satified, what is your favorite way to implement ?
Thank you.
Best way is:
BROWN = 10
BLOND = 20
RED = 30
HAIR_COLORS = (
(BROWN, 'braun'),
(BLOND, 'blond'),
(RED, 'red'))
hair_color = models.IntegerField(max_length=2, choices=HAIR_COLORS)
The reason for that is:
if obj.hair_color == BROWN instead if obj.hair_color == 10
or
obj.hair_color = BROWN compared to obj.hair_color = 10
Also better naming convention is recommended for example: prefix for the constants like EYES_BROWN and suffix for the choices like HAIR_COLORS_CHOICES. And these statements should reside outside the model for easier import in other parts in the project.