Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:07:32+00:00 2026-06-11T01:07:32+00:00

I have a problem with a model’s clean() method and basic field validation. here’s

  • 0

I have a problem with a model’s clean() method and basic field validation. here’s my model and the clean() method.

class Trial(models.Model):

    trial_start = DurationField()
    movement_start = DurationField()
    trial_stop = DurationField()


    def clean(self):
        from django.core.exceptions import ValidationError
        if not (self.movement_start >= self.trial_start):
            raise ValidationError('movement start must be >= trial start')
        if not (self.trial_stop >= self.movement_start):
            raise ValidationError('trial stop must be >= movement start')
        if not (self.trial_stop > self.trial_start):
            raise ValidationError('trial stop must be > trial start')

My clean() method checks whether certain values are in the correct range. If the user forget to fill out a field, e.g. movement_start, then I get an error:

can't compare datetime.timedelta to NoneType

I’m surprised that I get this error, since the original clean() function should be catching that missing entry (after all movement_start is a required field). So how can I the basic checking for missing values, and my custom check whether values are in certain ranges? Can this be done with model’s clean() method, or do I need to use Forms?

EDIT1 to make it more clear: trial_start, movement_start and trial_stop are all required fields. I need to write a clean() method which first checks that all three fields have been filled out, and then, check whether the values are in a certain range.

The following code for example DOES NOT work, since trial_start might be empty. I want to avoid having to check for the existence of each field – django should do that for me.

class TrialForm(ModelForm):

    class Meta:
        model = Trial

    def clean_movement_start(self):
        movement_start = self.cleaned_data["movement_start"]
        trial_start = self.cleaned_data["trial_start"]
        if not (movement_start >= trial_start):
            raise forms.ValidationError('movement start must be >= trial start')
        return self.cleaned_data["movement_start"] 

EDIT2 The reason that I wanted to add this check to the model’s clean() method is that objects that are created on the python shell, will automatically be checked for correct values. A form will be fine for views, but I need the value check also for the shell.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:07:34+00:00Added an answer on June 11, 2026 at 1:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one problem Looking below: I have this model: class Shoes(models.Model): shop =
I have problem with model built like this: class Topping(models.Model): name = models.CharField(max_length=30) class
I've got an interesting box-model problem here. I have a header full of links,
I have problem adding entity framework model to my project. Here is what I
I have problem with update listbox from view model class. I use Caliburn Micro
I have a problem where I dispatch an event in a model class which
in views/jump/index have problem. first rows: @model IEnumerable<WhislaMvc.Areas.Admin.Models.Jump> @{ ViewBag.Title = Index; } @model
I have problem with creating list of models. Suppose I've created model: > rp
I have a problem with automagic and related model data. I have 4 models:
I have problem with keeping my model (represented as List) in sync with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.