[Fixed]-Django: Cannot validate a form without an initial foreignkey value

1👍

If you are setting the patient in the view, then just leave the patient form out of the list of fields for you model form:

DiagnosisForm(forms.ModelForm):
    class Meta:
        model = Diagnosis
        fields = ('myfield1', 'myfield2', ...)

You can use exclude if you prefer:

DiagnosisForm(forms.ModelForm):
    class Meta:
        model = Diagnosis
        exclude = ('author', 'patient',)

The problem in your current form is that you have

    exclude = ['patient']
    exclude = ('author',)

The second exclude replaces the first. You should have:

    exclude = ['author', 'patient']

See the model form docs on selecting the fields to use for more info.

Leave a comment