Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayu-code-lab/c42cfdba2d273d99965b6847c9932089 to your computer and use it in GitHub Desktop.
Save bayu-code-lab/c42cfdba2d273d99965b6847c9932089 to your computer and use it in GitHub Desktop.
django add data attributes to choicefield select option
#region widgets.py
from django import forms
class CustomSelect(forms.widgets.Select):
def __init__(self, attrs=None, choices=(), modify_choices=()):
super(CustomSelect, self).__init__(attrs, choices=choices)
# set data
self.modify_choices = modify_choices
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
option = super(CustomSelect, self).create_option(name, value, label, selected, index, subindex, attrs)
for a, b, c, d in self.modify_choices:
#your statement
if value == a:
option['attrs']['color-code'] = b
option['attrs']['hex-code'] = c
option['attrs']['hex-code-style'] = d
return option
#endregion
#region forms.py
from django import forms
from . import models
from . import widgets as customWidget
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
get_car_variant_color_data = models.MyColorModels.objects.filter(variant=self.instance.color.id, is_deleted=False)
self.fields['color'] = forms.ModelChoiceField(
initial=None, queryset=get_car_variant_color_data,
widget=customWidget.CustomSelect(modify_choices=tuple(get_car_variant_color_data.values_list('id','code', 'color_alias__hex_code', 'color_alias__hex_code_style'))))
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment