TrueNoneField

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.db.models.fields import subclassing
from django.core import exceptions
from django.conf import settings
from django.db import models

class TrueNoneField(models.NullBooleanField):
    __metaclass__ = subclassing.SubfieldBase

    def db_type(self):
        if 'postgresql' in settings.DATABASE_ENGINE:
            return "boolean CHECK (%s <> false)" % self.get_attname_column()[1]
        else:
            return super(TrueNoneField, self).db_type()

    def to_python(self, value):
        if value in (True, 1):
            return True
        elif value in (False, 0, None):
            return None

        raise exceptions.ValidationError("This value must be either True or None.")

    def get_db_prep_save(self, value):
        return super(TrueNoneField, self).get_db_prep_save(self.to_python(value))

More like this

  1. RequiredNullBooleanField by wwu.housing 4 years, 2 months ago
  2. Improved Pickled Object Field by taavi223 3 years, 9 months ago
  3. True Unique Boolean Decorator by kunitoki 1 year, 3 months ago
  4. True Unique Boolean Model Decorator by kunitoki 1 year ago
  5. Add validation for 'unique' and 'unique_together' constraints to newforms created dynamically via form_for_model or form_for_instance by bikeshedder 5 years, 11 months ago

Comments

(Forgotten your password?)