Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

nickfrez / unb-uuiduser   python

Repository URL to install this package:

/ fields.py

from django.db import models


# TODO(nick): This needs to be pulled out into its own package.
class NullCharField(models.CharField):
  description = "CharField that transparently stores NULL for empty strings."

  def to_python(self, value):
    # Is this the value right out of the db, or an instance? If an instance,
    # just return the instance.
    if isinstance(value, models.CharField):
      return value
    return value or ''  # Transform NULLs to empty strings.

  def get_prep_value(self, value):
    return value or None  # Transform empty strings to NULLs.