[Answered ]-Queryset in ModelForm is properly limited initially, but not when form is submitted with errors

2👍

First off, it looks like you left off a bit – make_prescription_form returns a class, not a form instance, and you’re passing the class directly to the rendering in the GET path. I am assuming that’s a typo.

You’re not using your make_prescription_form wrapper in the POST path. The smallest change from this implementation would be:

def add_form(request):
    form_class = forms.make_prescription_form(request.user)
    if request.method == 'POST':
        form = form_class(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = form_class()
    return render(request, 'add_form.html', {'form': form})

As for other ways to do this – you can just set the form field’s queryset directly in your view.

forms.py
class PrescriptionForm(ModelForm):
    class Meta:
        model = models.Prescription

views.py
def add_form(request):
    if request.method == 'POST':
        form = PrescriptionForm(request.POST)
        form.fields['patient'].queryset = models.Patient.objects.filter(doctor=request.user)
        if form.is_valid():
            form.save()
    else:
        form = PrescriptionForm()
        form.fields['patient'].queryset = models.Patient.objects.filter(doctor=request.user)
    return render(request, 'add_form.html', {'form': form})

Or set doctor as an argument to PrescriptionForm’s __init__ and update the queryset there:

forms.py
class PrescriptionForm(ModelForm):
    class Meta:
        model = models.Prescription

    def __init__(self, *args, doctor=None, **kwargs):
        super(PrescriptionForm, self).__init__(*args, **kwargs)
        if self.doctor is not None:
            self.fields['patient'] = models.Patient.objects.filter(doctor=doctor)

views.py
def add_form(request):
    if request.method == 'POST':
        form = PrescriptionForm(request.POST, doctor=request.user)
        if form.is_valid():
            form.save()
    else:
        form = PrescriptionForm(doctor=request.user)
    return render(request, 'add_form.html', {'form': form})                

Leave a comment