| 1597 | |
| 1598 | |
| 1599 | class DateTimeField(DateField): |
| 1600 | empty_strings_allowed = False |
| 1601 | default_error_messages = { |
| 1602 | "invalid": _( |
| 1603 | "“%(value)s” value has an invalid format. It must be in " |
| 1604 | "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format." |
| 1605 | ), |
| 1606 | "invalid_date": _( |
| 1607 | "“%(value)s” value has the correct format " |
| 1608 | "(YYYY-MM-DD) but it is an invalid date." |
| 1609 | ), |
| 1610 | "invalid_datetime": _( |
| 1611 | "“%(value)s” value has the correct format " |
| 1612 | "(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) " |
| 1613 | "but it is an invalid date/time." |
| 1614 | ), |
| 1615 | } |
| 1616 | description = _("Date (with time)") |
| 1617 | |
| 1618 | # __init__ is inherited from DateField |
| 1619 | |
| 1620 | def _check_fix_default_value(self): |
| 1621 | """ |
| 1622 | Warn that using an actual date or datetime value is probably wrong; |
| 1623 | it's only evaluated on server startup. |
| 1624 | """ |
| 1625 | if not self.has_default(): |
| 1626 | return [] |
| 1627 | |
| 1628 | value = self.default |
| 1629 | if isinstance(value, (datetime.datetime, datetime.date)): |
| 1630 | return self._check_if_value_fixed(value) |
| 1631 | # No explicit date / datetime value -- no checks necessary. |
| 1632 | return [] |
| 1633 | |
| 1634 | def get_internal_type(self): |
| 1635 | return "DateTimeField" |
| 1636 | |
| 1637 | def to_python(self, value): |
| 1638 | if value is None: |
| 1639 | return value |
| 1640 | if isinstance(value, datetime.datetime): |
| 1641 | return value |
| 1642 | if isinstance(value, datetime.date): |
| 1643 | value = datetime.datetime(value.year, value.month, value.day) |
| 1644 | if settings.USE_TZ: |
| 1645 | # For backwards compatibility, interpret naive datetimes in |
| 1646 | # local time. This won't work during DST change, but we can't |
| 1647 | # do much about it, so we let the exceptions percolate up the |
| 1648 | # call stack. |
| 1649 | try: |
| 1650 | name = f"{self.model.__name__}.{self.name}" |
| 1651 | except AttributeError: |
| 1652 | name = "(unbound)" |
| 1653 | warnings.warn( |
| 1654 | f"DateTimeField {name} received a naive datetime ({value}) while " |
| 1655 | "time zone support is active.", |
| 1656 | RuntimeWarning, |
no outgoing calls