Anurag Rana
Anurag Rana

Reputation: 1466

key is not available in cleaned data in clean method of form in django

I have below ModelForm class.

from django.forms import ModelForm
from tenant.models import EventsModel
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django import forms

class EventsForm(ModelForm):
    class Meta:
        model = EventsModel
        fields = '__all__'

    def clean_start_date_time(self):
        # can not be before than now.
        data = self.cleaned_data
        print(data)
        start_date_time =  data.get("start_date_time")
        now_date_time = timezone.now()
        if start_date_time < now_date_time:
            raise forms.ValidationError(
                _('Start time has passed.'),
                code='invalid',
            )
        return data

    def clean(self):
        # end date can not be before or equal to start date time
        # data = super(EventsForm, self).clean()
        data = self.cleaned_data
        print(data)
        start_date_time =  data.get("start_date_time")
        end_date_time = data.get("end_date_time")
        if start_date_time >= end_date_time:
            raise forms.ValidationError(
                _('End time should be after start time.'),
                code='invalid',
            )

clean_start_date_time raised an error because start_date_time is in past. start_date_time is available in clean_start_date_time method but is not available in clean method. Why is this happening?

I printed the cleaned data in both the methods, there is difference in cleaned data. Why is this difference?

cleaned data in clean_start_date_time :

{'tenant_sys_id': None, 'name': 'dfghj', 'start_date_time': datetime.datetime(2016, 12, 21, 13, 20, 23)}

cleaned data in clean :

{'created_by': None, 'last_updated_when': None, 'last_updated_by': None, 'tenant_sys_id': None, 'notes': 'g', 'name': 'dfghj', 'created_when': None, 'end_date_time': datetime.datetime(2016, 12, 31, 14, 20, 29)}

Upvotes: 1

Views: 1547

Answers (2)

Ytsen de Boer
Ytsen de Boer

Reputation: 3107

According to the documentation, your function clean_start_date_time should return the cleaned field value start_date_time.

Your function now exits either with an exception, or with cleaned_data. That is the reason that cleaned_data is not the same in the clean method and the clean_start_date_time method:

The clean_fieldname() method is called on a form subclass – where fieldname is replaced with the name of the form field attribute.

...

This method should return the cleaned value obtained from cleaned_data, regardless of whether it changed anything or not.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599540

It's not there because you raised an error in clean_start_date_time instead of returning. Note that you did exactly what you are supposed to do. You should never assume that a field will be present in clean; the same thing would have happened if the user had not filled in data for that field. Check the value is present before doing anything else.

Upvotes: 2

Related Questions