0

I have a model:

class PastStudy(Model):
    grade_average = FloatField(null=True)

I have a modelform as below:

class PastStudyForm(ModelForm):

    class Meta:
        model = PastStudy
        fields = ('grade_average', )

What I have in view:

...
if request.POST:
    past_study_form = PastStudyForm(request.POST)

    if past_study_form.is_valid():
        return HttpResponse(past_study_form.cleaned_data['grade_average'])

else:
    profile_filter_past_study_form = ProfileFilterPastStudyForm()
...

What I need is that I want to write a clean method for PastStudyForm so that in case I entered 90 as grade average, HttpResponse converts it two 0-20 grading scheme and returns 18. I tried this and I was still getting 90 not 18

class PastStudyForm(ModelForm):

    class Meta:
        model = PastStudy
        fields = ('grade_average', )

    def clean(self):
        cleaned_data = super().clean()
        grade_average = self.cleaned_data['grade_average']
        self.cleaned_data['grade_average'] = grade_average/5
        return cleaned_data

and This:

class PastStudyForm(ModelForm):

    class Meta:
        model = PastStudy
        fields = ('grade_average', )

    def clean_grade_average(self):
        grade_average = self.cleaned_data['grade_average']
        data = grade_average/5
        return data

However, I still get 90. I also have tried a few other methods but I still was getting 90 in HttpResponse

Maybe using clean method be wrong and I should do something else!

The real code is huge and I have summarized it in here and other aspects of the problem are not explained in here. That is why I prefer and expect to get a response in which I am advised how to it in the form definition, not other methods such as converting it in the view.

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • What happens if you return the `past_study_form` in your response, and then in the template write `past_study_form.grade_average`? Same result? – Daniel Holmes Aug 05 '19 at 07:49
  • I do not have a template for request.POST . I am returning it as an HttpResponse. Please stick to this situation and tell what to write in the HttpResponse to test and let you know what will be the result – Amin Ba Aug 05 '19 at 07:59

1 Answers1

1

in your clean method, you assign the result of your calculation method into self.cleaned_data,
while you are returning cleaned_data not self.cleaned_data.
it is different variable.

try this instead:

self.cleaned_data = super().clean()
grade_average = self.cleaned_data['grade_average']
self.cleaned_data['grade_average'] = grade_average/5
return self.cleaned_data
lieahau
  • 498
  • 4
  • 9
  • It did not work and I am still getting the same result as before – Amin Ba Aug 05 '19 at 07:15
  • 1
    It seems that clean method is irrelevant to this situation. I found this in the documentation: Form.clean() Implement a clean() method on your Form when you must add custom validation for fields that are interdependent. See Cleaning and validating fields that depend on each other for example usage. – Amin Ba Aug 05 '19 at 07:27