Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned integer field in django

Any idea how to use an unsigned integer field in Django? By this I mean a field inside a model with a range from 0 to 4294967295 and stores 32 bit. This definition excludes the PositiveIntegerField because it only has a range from 0 to 2147483647. I also prefer not to use a bigIntegerField as that would be a huge waste of memory.

like image 381
Vjeetje Avatar asked Sep 20 '13 20:09

Vjeetje


1 Answers

Gave it a go in creating a custom UnsignedIntegerField. Tested only manually for inserting unsigned 32-bit integers.

from django.core import validators
from django.db import models
from django.utils.functional import cached_property


class UnsignedIntegerField(models.IntegerField):

    MAX_INT = 4294967295

    @cached_property
    def validators(self):
        # These validators can't be added at field initialization time since
        # they're based on values retrieved from `connection`.
        validators_ = [*self.default_validators, *self._validators]
        min_value, max_value = 0, self.MAX_INT
        if (min_value is not None and not
            any(isinstance(validator, validators.MinValueValidator) and
                validator.limit_value >= min_value for validator in validators_)):
            validators_.append(validators.MinValueValidator(min_value))
        if (max_value is not None and not
            any(isinstance(validator, validators.MaxValueValidator) and
                validator.limit_value <= max_value for validator in validators_)):
            validators_.append(validators.MaxValueValidator(max_value))
        return validators_

    def db_type(self, connection):
        return "INTEGER UNSIGNED"

    def get_internal_type(self):
        return "UnsignedIntegerField"

    def formfield(self, **kwargs):
        return super().formfield(**{
            'min_value': 0,
            'max_value': self.MAX_INT,
            **kwargs,
        })
like image 140
kodemartin Avatar answered Sep 19 '22 08:09

kodemartin