MCPcopy
hub / github.com/django/django / _check_if_value_fixed

Method _check_if_value_fixed

django/db/models/fields/__init__.py:1429–1461  ·  view source on GitHub ↗

Check if the given value appears to have been provided as a "fixed" time value, and include a warning in the returned list if it does. The value argument must be a date object or aware/naive datetime object. If now is provided, it must be a naive datetime object.

(self, value, now=None)

Source from the content-addressed store, hash-verified

1427 # Concrete subclasses use this in their implementations of
1428 # _check_fix_default_value().
1429 def _check_if_value_fixed(self, value, now=None):
1430 """
1431 Check if the given value appears to have been provided as a "fixed"
1432 time value, and include a warning in the returned list if it does. The
1433 value argument must be a date object or aware/naive datetime object. If
1434 now is provided, it must be a naive datetime object.
1435 """
1436 if now is None:
1437 now = _get_naive_now()
1438 offset = datetime.timedelta(seconds=10)
1439 lower = now - offset
1440 upper = now + offset
1441 if isinstance(value, datetime.datetime):
1442 value = _to_naive(value)
1443 else:
1444 assert isinstance(value, datetime.date)
1445 lower = lower.date()
1446 upper = upper.date()
1447 if lower <= value <= upper:
1448 return [
1449 checks.Warning(
1450 "Fixed default value provided.",
1451 hint=(
1452 "It seems you set a fixed date / time / datetime "
1453 "value as default for this field. This may not be "
1454 "what you want. If you want to have the current date "
1455 "as default, use `django.utils.timezone.now`"
1456 ),
1457 obj=self,
1458 id="fields.W161",
1459 )
1460 ]
1461 return []
1462
1463
1464class DateField(DateTimeCheckMixin, Field):

Callers 3

Calls 2

_get_naive_nowFunction · 0.85
_to_naiveFunction · 0.85

Tested by

no test coverage detected