SelectDateWidget when DateField is not required

60 views
Skip to first unread message

Nonverbis M

unread,
Jun 12, 2017, 6:28:29 AM6/12/17
to Django users

Django 1.11.2

I'd like to use SelectDateWidget.


class FrameDate(models.Model):
    through_date
= models.DateField(null=True,
                                    blank
=True,
                                    verbose_name
=_("through"))
   
...

class FrameDateForm(ModelForm):

   
def clean_through_date(self):
       
pass # Breakpoint

   
class Meta:
            model
= FrameDate
            exclude
= []

            years
= range(1800, datetime.datetime.now().year + 1)

            widgets
= {
               
'frame': forms.HiddenInput(),
               
'from_date': forms.SelectDateWidget(years=years),
               
'through_date': forms.SelectDateWidget(years=years)
           
}



As we can see, this DateField is not required. And have an empty choice at the top of the list ("---").

The problem is that when a user inputs only a month we'll get ValueError springing out to the user in case of Debug=True.

There is that method clean_through_date and the break point in it. But in case of incomplete set of month, day and year the program will not even call this clean_through_date() method. This method is called in two cases:

1) if the through_date is completely empty.

2) if it is completely filled.

If it is partially filled, form validation is ignored completely. The program explodes at CreateView.

Could you comment on it? And give me a kick here: how to warn the user that s/he must either fill month, day and year or don't touch the through_date at all.

Melvyn Sopacua

unread,
Jun 12, 2017, 7:25:27 PM6/12/17
to django...@googlegroups.com

On Monday 12 June 2017 03:28:29 Nonverbis M wrote:

 

> There is that method clean_through_date and the break point in it. But

> in case of incomplete set of month, day and year the program will not

> even call this clean_through_date() method. This method is called in

> two cases:

>

> 1) if the through_date is completely empty.

>

> 2) if it is completely filled.

>

> If it is partially filled, form validation is ignored completely. The

> program explodes at CreateView.

 

Do you have a backtrace?

 

--

Melvyn Sopacua

Tim Graham

unread,
Jun 30, 2017, 8:45:49 AM6/30/17
to Django users
I can't reproduce a crash. If I select only a month, I see a validation error, "Enter a valid date."
Reply all
Reply to author
Forward
0 new messages