32-bit integer field.
| 326 | |
| 327 | |
| 328 | class IntField(BaseField): |
| 329 | """32-bit integer field.""" |
| 330 | |
| 331 | def __init__(self, min_value=None, max_value=None, **kwargs): |
| 332 | """ |
| 333 | :param min_value: (optional) A min value that will be applied during validation |
| 334 | :param max_value: (optional) A max value that will be applied during validation |
| 335 | :param kwargs: Keyword arguments passed into the parent :class:`~mongoengine.BaseField` |
| 336 | """ |
| 337 | self.min_value, self.max_value = min_value, max_value |
| 338 | super().__init__(**kwargs) |
| 339 | |
| 340 | def to_python(self, value): |
| 341 | try: |
| 342 | value = int(value) |
| 343 | except (TypeError, ValueError): |
| 344 | pass |
| 345 | return value |
| 346 | |
| 347 | def validate(self, value): |
| 348 | try: |
| 349 | value = int(value) |
| 350 | except (TypeError, ValueError): |
| 351 | self.error("%s could not be converted to int" % value) |
| 352 | |
| 353 | if self.min_value is not None and value < self.min_value: |
| 354 | self.error("Integer value is too small") |
| 355 | |
| 356 | if self.max_value is not None and value > self.max_value: |
| 357 | self.error("Integer value is too large") |
| 358 | |
| 359 | def prepare_query_value(self, op, value): |
| 360 | if value is None: |
| 361 | return value |
| 362 | |
| 363 | return super().prepare_query_value(op, int(value)) |
| 364 | |
| 365 | |
| 366 | class LongField(IntField): |