Files
edx-platform/openedx/core/djangolib/fields.py
aliciaerwin 140fb1394e INCR-166 Run python-modernize on openedx/core/djangolib (#20473)
* INCR-166 Run python-modernize on openedx/core/djangolib

* INCR-166 disabled harmless errors and added docstring
2019-05-09 13:59:48 -04:00

60 lines
1.5 KiB
Python

"""
Custom Django fields.
"""
from __future__ import absolute_import
from django.db import models
class CharNullField(models.CharField):
"""
CharField that stores NULL but returns ''
"""
description = "CharField that stores NULL but returns ''"
def to_python(self, value):
"""Converts the value into the correct Python object."""
if isinstance(value, models.CharField):
return value
if value is None:
return ""
else:
return value
def get_db_prep_value(self, value, connection, prepared=False):
"""Converts value to a backend-specific value."""
if not prepared:
value = self.get_prep_value(value)
if value == "":
return None
else:
return value
class BigAutoField(models.AutoField):
"""
AutoField that uses BigIntegers.
This exists in Django as of version 1.10.
"""
def db_type(self, connection):
"""
The type of the field to insert into the database.
"""
conn_module = type(connection).__module__
if "mysql" in conn_module:
return "bigint AUTO_INCREMENT"
elif "postgres" in conn_module:
return "bigserial"
else:
return super(BigAutoField, self).db_type(connection)
def rel_db_type(self, connection):
"""
The type to be used by relations pointing to this field.
Not used until Django 1.10.
"""
return "bigint"