How do you create a dynamic form field in Django?

Viewed 50

I want to create a dynamic form field that always filters a model and shows the filtered elements in a list in the form. Here is the code I have so far:

class OrganisorClassStatusUpdateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(OrganisorClassStatusUpdateForm, self).__init__(*args, **kwargs)
        self.fields['off_day_student'] = forms.ModelChoiceField(
            queryset=Class.objects.filter(teacher=self.object.teacher).filter(is_new_set=True).filter(time=self.object.time).order_by('-date')[:10]
        )

The problem I have with this code is that the self.object refers to the form itself, not the model I am trying to update. This is important since I need to filter the queryset using some of the information in the model I am trying to update. Please ask me any question if you need anything. Thanks a lot.

0 Answers
Related