| 1462 | |
| 1463 | |
| 1464 | class DateField(DateTimeCheckMixin, Field): |
| 1465 | empty_strings_allowed = False |
| 1466 | default_error_messages = { |
| 1467 | "invalid": _( |
| 1468 | "“%(value)s” value has an invalid date format. It must be " |
| 1469 | "in YYYY-MM-DD format." |
| 1470 | ), |
| 1471 | "invalid_date": _( |
| 1472 | "“%(value)s” value has the correct format (YYYY-MM-DD) " |
| 1473 | "but it is an invalid date." |
| 1474 | ), |
| 1475 | } |
| 1476 | description = _("Date (without time)") |
| 1477 | |
| 1478 | def __init__( |
| 1479 | self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs |
| 1480 | ): |
| 1481 | self.auto_now, self.auto_now_add = auto_now, auto_now_add |
| 1482 | if auto_now or auto_now_add: |
| 1483 | kwargs["editable"] = False |
| 1484 | kwargs["blank"] = True |
| 1485 | super().__init__(verbose_name, name, **kwargs) |
| 1486 | |
| 1487 | def _check_fix_default_value(self): |
| 1488 | """ |
| 1489 | Warn that using an actual date or datetime value is probably wrong; |
| 1490 | it's only evaluated on server startup. |
| 1491 | """ |
| 1492 | if not self.has_default(): |
| 1493 | return [] |
| 1494 | |
| 1495 | value = self.default |
| 1496 | if isinstance(value, datetime.datetime): |
| 1497 | value = _to_naive(value).date() |
| 1498 | elif isinstance(value, datetime.date): |
| 1499 | pass |
| 1500 | else: |
| 1501 | # No explicit date / datetime value -- no checks necessary |
| 1502 | return [] |
| 1503 | # At this point, value is a date object. |
| 1504 | return self._check_if_value_fixed(value) |
| 1505 | |
| 1506 | def deconstruct(self): |
| 1507 | name, path, args, kwargs = super().deconstruct() |
| 1508 | if self.auto_now: |
| 1509 | kwargs["auto_now"] = True |
| 1510 | if self.auto_now_add: |
| 1511 | kwargs["auto_now_add"] = True |
| 1512 | if self.auto_now or self.auto_now_add: |
| 1513 | del kwargs["editable"] |
| 1514 | del kwargs["blank"] |
| 1515 | return name, path, args, kwargs |
| 1516 | |
| 1517 | def get_internal_type(self): |
| 1518 | return "DateField" |
| 1519 | |
| 1520 | def to_python(self, value): |
| 1521 | if value is None: |
no outgoing calls