Reputation: 4664
I have a form in django views.py
.
diagnosis.patient
is a foreignkey of demographics.patient_id
.
my_diagnosis
form is not valid because patient is empty. Is there a way to fix this?
def input(request):
context = RequestContext(request)
print context
ret = cache.get('input-rendered')
if request.method == 'POST':
my_demographics = DemographicForm(request.POST, prefix="demo")
my_diagnosis = DiagnosisForm(request.POST, prefix='diag')
if (my_demographics.is_valid() ):
print "dem and diag validation"
my_demographics_object = my_demographics.save(commit=False)
my_demographics_object.author = request.user
my_demographics_object.save()
#my_diagnosis = DiagnosisForm(request.POST, prefix='diag', initial={'patient':my_demographics_object.patient_id} )
print "my dem id"
print my_demographics_object.patient_id
if (my_diagnosis.is_valid()):
my_diagnosis_object=my_diagnosis.save(commit=False)
my_diagnosis_object.patient = my_demographics_object.patient_id
my_diagnosis_object.author = request.user
my_diagnosis_object.save()
This is my Diagnosis
form in forms.py
:
class DiagnosisForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(DiagnosisForm, self).__init__(*args, **kwargs)
self.fields['diagnosis_circumstances_date']= forms.DateField(label=('Date'),required=False,
widget=DateTimePicker(options={"format": "YYYY-MM-DD",
"pickTime": False,
"startDate": "1900-01-01"}))
self.helper=FormHelper(form=self)
self.fields['icd_10_desc']= forms.ModelChoiceField(queryset=icd_10.objects.all(),
widget=autocomplete_light.ChoiceWidget("icd_10Autocomplete"))
self.fields['icd_10_desc'].label = "ICD-10 description"
diagnosis_option_value = (
('b-thalassaemia syndromes', 'b-thalassaemia syndromes',),
('a-thalassaemia syndromes', 'a-thalassaemia syndromes'),
('Sickle cell syndromes', 'Sickle cell syndromes'),
('Other haemoglobin variants','Other haemoglobin variants'),
('Red cell membrane disorders','Red cell membrane disorders'),
('Red cell enzyme disorders','Red cell enzyme disorders'),
('Congenital dyserythropoietic anaemias','Congenital dyserythropoietic anaemias')
)
self.fields['diagnosis_option']=forms.MultipleChoiceField(choices=diagnosis_option_value, widget=forms.CheckboxSelectMultiple())
diagnosis_circumstances_value = (
('Antenatal diagnosis','Antenatal diagnosis'),
('Neonatal diagnosis','Neonatal diagnosis'),
('By the presence of affected related','By the presence of affected related'),
('Clinical diagnosis', 'Clinical diagnosis'),
('Other','Other')
)
self.fields['diagnosis_circumstances']=forms.MultipleChoiceField(choices=diagnosis_circumstances_value, widget=forms.CheckboxSelectMultiple())
#self.fields['patient'].queryset = Demographic.objects.filter(patient_id=self.instance.patient)
self.helper.field_class = 'col-md-8'
self.helper.label_class = 'col-md-3'
#self.helper.form_class = 'forms-horizontal'
self.helper.layout = Layout(
Fieldset (
# 'patient',
'<b>Diagnosis information</b>',
Div(
#HTML(u'<div class="col-md-2"></div>'),
Div('age_of_diagnosis',css_class='col-md-6'),
Div('age_at_onset_of_symptoms',css_class="col-md-6"),
css_class='row',
),
'diagnosis_option',
'record_of_genotype',
'icd_10_desc',
'icd_10_code',
'orpha_code',
'comment',
),
FormActions(
Submit('submit', "Save changes"),
Submit('cancel',"Cancel")
),
)
self.helper.form_tag = False
self.helper.form_show_labels = True
class Meta:
model = Diagnosis
exclude = ['patient']
exclude = ('author',)
list_display = ('title', 'pub_date', 'author')
This is the result of my_diagnosis.errors
:
<ul class="errorlist"><li>patient<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
Upvotes: 0
Views: 32
Reputation: 308839
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.
Upvotes: 1