#24897: Choices don't really work with DurationField
----------------------------+---------------------------
     Reporter:  zauddelig   |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Forms       |    Version:  1.8
     Severity:  Normal      |   Keywords:  DurationField
 Triage Stage:  Unreviewed  |  Has patch:  1
Easy pickings:  0           |      UI/UX:  0
----------------------------+---------------------------
 When one tries to use DurationField with choices and a time span longer
 than a day the field will always raise a ValidationError.

 {{{
 choices = (timedelta(days=7), "one week")
 }}}

 The problem is in the `to_python` method wich calls
 `django.utils.dateparse.parse_duration` this methond use the following
 regex:
 {{{
 standard_duration_re = re.compile(
     r'^'
     r'(?:(?P<days>-?\d+) )?'
     r'((?:(?P<hours>\d+):)(?=\d+:\d+))?'
     r'(?:(?P<minutes>\d+):)?'
     r'(?P<seconds>\d+)'
     r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?'
     r'$'
 )
 }}}

 But is not standard in the view that we are using timedelta objects, whose
 default rapresentation is : `%d days, %h:%m:%s`. So I propose to change
 the regex in with the following:
 {{{
  standard_duration_re = re.compile(
     r'^'
     r'(?:(?P<days>-?\d+) (days, )?)?'
     r'((?:(?P<hours>\d+):)(?=\d+:\d+))?'
     r'(?:(?P<minutes>\d+):)?'
     r'(?P<seconds>\d+)'
     r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?'
     r'$'
 )
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24897>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.831d3a253b19a66d3907d75b0a8e555d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to