Floating point number field.
| 371 | |
| 372 | |
| 373 | class FloatField(BaseField): |
| 374 | """Floating point number field.""" |
| 375 | |
| 376 | def __init__(self, min_value=None, max_value=None, **kwargs): |
| 377 | """ |
| 378 | :param min_value: (optional) A min value that will be applied during validation |
| 379 | :param max_value: (optional) A max value that will be applied during validation |
| 380 | :param kwargs: Keyword arguments passed into the parent :class:`~mongoengine.BaseField` |
| 381 | """ |
| 382 | self.min_value, self.max_value = min_value, max_value |
| 383 | super().__init__(**kwargs) |
| 384 | |
| 385 | def to_python(self, value): |
| 386 | try: |
| 387 | value = float(value) |
| 388 | except ValueError: |
| 389 | pass |
| 390 | return value |
| 391 | |
| 392 | def validate(self, value): |
| 393 | if isinstance(value, int): |
| 394 | try: |
| 395 | value = float(value) |
| 396 | except OverflowError: |
| 397 | self.error("The value is too large to be converted to float") |
| 398 | |
| 399 | if not isinstance(value, float): |
| 400 | self.error("FloatField only accepts float and integer values") |
| 401 | |
| 402 | if self.min_value is not None and value < self.min_value: |
| 403 | self.error("Float value is too small") |
| 404 | |
| 405 | if self.max_value is not None and value > self.max_value: |
| 406 | self.error("Float value is too large") |
| 407 | |
| 408 | def prepare_query_value(self, op, value): |
| 409 | if value is None: |
| 410 | return value |
| 411 | |
| 412 | return super().prepare_query_value(op, float(value)) |
| 413 | |
| 414 | |
| 415 | class DecimalField(BaseField): |
no outgoing calls
no test coverage detected