I am trying to show inactive users as "disabled" in my form's Select widget.
I have a worker who is a django user model.
models.py
class Task(models.Model):
worker = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING, blank=True, null=True,related_name='worker')
It is represented by a ModelForm, using a subclass to display the full name of the user.
forms.py
class UserModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.get_full_name()
class TaskForm(forms.ModelForm):
worker = UserModelChoiceField(queryset=User.objects.filter(is_active=1).order_by('first_name'),widget=forms.Select(attrs={'class':'form-control'}),required=False)
class Meta:
model = Task
fields = ['worker']
Currently, the filter is_active=1 means that inactive users simply don't show in the list and where they've already been selected it appears as "---".
My ideal would be they appear but are greyed out so they are presented but can't be selected.
From reviewing https://djangosnippets.org/snippets/2453/
Which I found in "Disabled" option for choiceField - Django
I was able to conclude that the subclass of the select should work. However I can't tell how to get between the queryset and the widget in order to achieve the expected result. Reading suggests the render method on the widget might be the way but I couldn't find examples of how to pass the information or exactly where create_option is invoked.