2👍
✅
You can use django-autocomplete-light
to do this. This basically provides autocompletion using a client library such as select2
. But it also provides a way to filter results based on another field.
You should likely also make a server verification in the form clean:
class MyForm(forms.Form):
project = forms.ModelChoiceField(queryset=Project.objects.all())
task = forms.Model.ChoicField(queryset=Task.objects.all())
def clean(self):
project = self.cleaned_data['project']
task = self.cleaned_data['task']
if task.project_id != project.pk:
raise forms.ValidationError(
"The selected task does not belong to the selected project.")
Source:stackexchange.com