I have a problem with a model\'s clean()
method and basic field validation. here\'s my model and the clean()
method.
class Trial(mo
I guess that's the way to go:
class TrialForm(ModelForm):
class Meta:
model = Trial
def clean(self):
data = self.cleaned_data
if not ('movement_start' in data.keys() and 'trial_start' in data.keys() and 'trial_stop' in data.keys()):
raise forms.ValidationError("Please fill out missing fields.")
trial_start = data['trial_start']
movement_start = data['movement_start']
trial_stop = data['trial_stop']
if not (movement_start >= trial_start):
raise forms.ValidationError('movement start must be >= trial start')
if not (trial_stop >= movement_start):
raise forms.ValidationError('trial stop must be >= movement start')
if not (trial_stop > trial_start):
raise forms.ValidationError('trial stop must be > trial start')
return data
EDIT the downside of this approach is, that value checking will only work if I create objects through the form. Objects that are created on the python shell won't be checked.