|
11 | 11 | from django.core.validators import RegexValidator
|
12 | 12 | from django.forms import (
|
13 | 13 | BooleanField, CharField, CheckboxSelectMultiple, ChoiceField, DateField,
|
14 |
| - EmailField, FileField, FloatField, Form, forms, HiddenInput, IntegerField, |
15 |
| - MultipleChoiceField, MultipleHiddenInput, MultiValueField, |
| 14 | + DateTimeField, EmailField, FileField, FloatField, Form, forms, HiddenInput, |
| 15 | + IntegerField, MultipleChoiceField, MultipleHiddenInput, MultiValueField, |
16 | 16 | NullBooleanField, PasswordInput, RadioSelect, Select, SplitDateTimeField,
|
17 |
| - Textarea, TextInput, ValidationError, widgets, |
| 17 | + Textarea, TextInput, TimeField, ValidationError, widgets |
18 | 18 | )
|
19 | 19 | from django.forms.utils import ErrorList
|
20 | 20 | from django.http import QueryDict
|
@@ -1321,6 +1321,29 @@ class UserRegistration(Form):
|
1321 | 1321 | self.assertEqual(bound['password'].value(), 'foo')
|
1322 | 1322 | self.assertEqual(unbound['password'].value(), None)
|
1323 | 1323 |
|
| 1324 | + def test_initial_datetime_values(self): |
| 1325 | + now = datetime.datetime.now() |
| 1326 | + # Nix microseconds (since they should be ignored). #22502 |
| 1327 | + now_no_ms = now.replace(microsecond=0) |
| 1328 | + if now == now_no_ms: |
| 1329 | + now = now.replace(microsecond=1) |
| 1330 | + |
| 1331 | + def delayed_now(): |
| 1332 | + return now |
| 1333 | + |
| 1334 | + def delayed_now_time(): |
| 1335 | + return now.time() |
| 1336 | + |
| 1337 | + class DateTimeForm(Form): |
| 1338 | + auto_timestamp = DateTimeField(initial=delayed_now) |
| 1339 | + auto_time_only = TimeField(initial=delayed_now_time) |
| 1340 | + supports_microseconds = DateTimeField(initial=delayed_now, widget=TextInput) |
| 1341 | + |
| 1342 | + unbound = DateTimeForm() |
| 1343 | + self.assertEqual(unbound['auto_timestamp'].value(), now_no_ms) |
| 1344 | + self.assertEqual(unbound['auto_time_only'].value(), now_no_ms.time()) |
| 1345 | + self.assertEqual(unbound['supports_microseconds'].value(), now) |
| 1346 | + |
1324 | 1347 | def test_help_text(self):
|
1325 | 1348 | # You can specify descriptive text for a field by using the 'help_text' argument)
|
1326 | 1349 | class UserRegistration(Form):
|
|
0 commit comments