我有一个客户和治疗数据库,每个客户可以有多种治疗方法。 问题是,治疗在DB中丢失了其客户价值,而不是客户的ID,该值只是NULL。
这在以下代码中发生:
def get_treatment(request, treatment_id):
customer = Treatment.objects.get(id=treatment_id).customer
if request.POST:
form = TreatmentForm(request.POST, instance=Treatment.objects.get(id=treatment_id))
if form.is_valid():
form.save() #***This is where it happens***
return HttpResponseRedirect('/')
else:
form = TreatmentForm(instance=Treatment.objects.get(id=treatment_id))
return render_to_response('new_treatment.html',
{"form" : form,
"customer_name" : customer,
},
RequestContext(request))
任何人都可以解释这种奇怪的行为吗?
class TreatmentForm(forms.ModelForm):
class Meta:
model = Treatment
fields = "__all__"
BOOL_CHOICES = (
(True, 'has'),
(False, 'none')
)
customer = forms.ModelChoiceField(queryset=Customer.objects.all(), widget=forms.RadioSelect(renderer=HorizontalRadioRenderer), required=False)
# Looking inspections
skin_hue_finding = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinHue.objects.all(), initial=0)
skin_hue_area = forms.CharField(max_length=200, required=False)
skin_hue_comments = forms.CharField(max_length=200, required=False)
skin_color_finding = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinColor.objects.all(), initial=0)
skin_color_area = forms.CharField(max_length=200, required=False)
skin_color_comments = forms.CharField(max_length=200, required=False)
pores_finding = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=Pores.objects.all(), initial=0)
pores_area = forms.ModelMultipleChoiceField(queryset=GlobalArea.objects.all(), widget=forms.CheckboxSelectMultiple(renderer=HorizontalCheckboxRenderer), required=False)
pores_comments = forms.CharField(max_length=200, required=False)
comdones_finding = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES))
comdones_area = forms.ModelMultipleChoiceField(queryset=GlobalArea.objects.all(), widget=forms.CheckboxSelectMultiple(renderer=HorizontalCheckboxRenderer), required=False)
comdones_comments = forms.CharField(max_length=200, required=False)
wrinkles_finding = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
wrinkles_area = forms.ModelMultipleChoiceField(queryset=WrinklesArea.objects.all(), widget=forms.CheckboxSelectMultiple(renderer=HorizontalCheckboxRenderer), required=False)
wrinkles_mimics = forms.BooleanField(required=False)
wrinkles_age = forms.BooleanField(required=False)
wrinkles_comments = forms.CharField(max_length=200, required=False)
# Touching
muscle_tonus = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=MuscleTonus.objects.all(), initial=0)
skin_torgor = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinTorgor.objects.all(), initial=0)
skin_thinkness = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinThickness.objects.all(), initial=0)
skin_touch = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinTouch.objects.all(), initial=0)
# Skin Problems
papules = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
papules_area = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinProblemAreas.objects.all(), required=False, initial=0)
papules_comments = forms.CharField(max_length=200, required=False)
postules = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
postules_area = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinProblemAreas.objects.all(), required=False, initial=0)
postules_comments = forms.CharField(max_length=200, required=False)
scars = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
scars_area = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=SkinProblemAreas.objects.all(), required=False, initial=0)
scars_comments = forms.CharField(max_length=200, required=False)
malium = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
malium_area = forms.ModelChoiceField(widget=RadioSelect(renderer=HorizontalRadioRenderer), queryset=MaliumAreas.objects.all(), required=False)
malium_comments = forms.CharField(max_length=200, required=False)
pigmentation = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
pigmentation_area = forms.CharField(max_length=200, required=False)
pigmentation_comments = forms.CharField(max_length=200, required=False)
nebuses = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
nebuses_area = forms.CharField(max_length=200, required=False)
nebuses_comments_color = forms.CharField(max_length=200, required=False)
nebuses_comments_hair = forms.CharField(max_length=200, required=False)
macules = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
macules_area = forms.CharField(max_length=200, required=False)
macules_comments = forms.CharField(max_length=200, required=False)
hypermia = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
hypermia_area = forms.CharField(max_length=200, required=False)
hypermia_comments = forms.CharField(max_length=200, required=False)
coproz = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
coproz_area = forms.CharField(max_length=200, required=False)
coproz_comments = forms.CharField(max_length=200, required=False)
hersotism = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
hersotism_area = forms.CharField(max_length=200, required=False)
hersotism_comments = forms.CharField(max_length=200, required=False)
other_problems = forms.NullBooleanField(widget=RadioSelect(renderer=HorizontalRadioRenderer, choices=BOOL_CHOICES), required=True)
other_problems_area = forms.CharField(max_length=200, required=False)
other_problems_comments = forms.CharField(max_length=200, required=False)
skin_diagnosis = forms.ModelMultipleChoiceField(queryset=DiagnosisCategory.objects.all(), widget=forms.CheckboxSelectMultiple(renderer=HorizontalCheckboxRenderer), required=True)
diagnosis_comment = forms.CharField(widget=forms.TextInput(attrs={'size':55}), max_length=200, required=False)
treatment_purpose = forms.CharField(widget=forms.TextInput(attrs={'size':55}), max_length=200)
customer_guidance = forms.CharField(widget=forms.Textarea(), max_length=200, required=False)
customer_recommendations = forms.CharField(widget=forms.Textarea(), max_length=200, required=False)
next_treatment_date = forms.DateField(required=True, input_formats=["%d-%m-%Y"], widget=forms.DateInput(format='%d-%m-%Y'))
next_treatment_comments = forms.CharField(widget=forms.Textarea(), max_length=200, required=True)
def __init__(self, *args, **kwargs):
super(TreatmentForm, self).__init__(*args, **kwargs)
self.fields["next_treatment_date"].widget.attrs["class"] = "datepicker"
def clean(self):
cleaned_data = super(TreatmentForm, self).clean()
validation_dict = {}
data = self.cleaned_data
if data.get("comdones_finding") == True and len(data.get("comdones_area")) == 0:
validation_dict["comdones_area"] = ["This field is required",]
# Check Boolean fields
if data.get("comdones_finding") is None:
validation_dict["comdones_finding"] = ["This field is required",]
if data.get("wrinkles_finding") is None:
validation_dict["wrinkles_finding"] = ["This field is required",]
if data.get("papules") is None:
validation_dict["papules"] = ["This field is required",]
if data.get("postules") is None:
validation_dict["postules"] = ["This field is required",]
if data.get("scars") is None:
validation_dict["scars"] = ["This field is required",]
if data.get("malium") is None:
validation_dict["malium"] = ["This field is required",]
if data.get("pigmentation") is None:
validation_dict["pigmentation"] = ["This field is required",]
if data.get("nebuses") is None:
validation_dict["nebuses"] = ["This field is required",]
if data.get("macules") is None:
validation_dict["macules"] = ["This field is required",]
if data.get("hypermia") is None:
validation_dict["hypermia"] = ["This field is required",]
if data.get("coproz") is None:
validation_dict["coproz"] = ["This field is required",]
if data.get("hersotism") is None:
validation_dict["hersotism"] = ["This field is required",]
if data.get("other_problems") is None:
validation_dict["other_problems"] = ["This field is required",]
if validation_dict:
raise forms.ValidationError(validation_dict)
return data
答案 0 :(得分:0)
我的猜测是,您在发布表单时没有为客户提交值。空白是有效值,因为您有required=False
。因此,Django在保存表单时将客户设置为None
。
如果您不希望customer
字段可编辑,则根本不应将其包含在表单中。您可以使用exclude
。
class TreatmentForm(forms.ModelForm):
class Meta:
model = Treatment
exclude = ['customer']
使用exclude
可能会有点危险。您将来可能会向模型添加私有字段,并且会因为您忘记将其添加到exclude
而自动添加到表单中。您可以设置fields
,然后退出customer
。
class TreatmentForm(forms.ModelForm):
class Meta:
model = Treatment
fields = ['skin_hue_finding', 'skin_hue_area', ... <all other fields>]
但是在这种情况下你可能不想这样做,因为你有很多字段。