I have to add title attribute to options of the ModelChoiceField. Here is my admin code for that:
class LocModelForm(forms.ModelForm):
def __init__(s
Here is a solution if you want to use the instance to set the attribute value.
class IconSelectWidget(forms.Select):
def create_option(self, name, value, *args, **kwargs):
option = super().create_option(name, value, *args, **kwargs)
if value:
icon = self.choices.queryset.get(pk=value) # get icon instance
option['attrs']['title'] = icon.title # set option attribute
return option
class LocModelForm(forms.ModelForm):
icons = forms.ModelChoiceField(
queryset=Photo.objects.filter(galleries__title_slug='markers'),
widget=IconSelectWidget
)