Skip to content

Commit c49d57a

Browse files
committed
Patch CharField/TextField to handle None as empty string and add none_to_empty converter
1 parent 746555e commit c49d57a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

gaussdb_django/base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.conf import settings
1212
from django.core.exceptions import ImproperlyConfigured
1313
from django.db import DatabaseError as WrappedDatabaseError
14-
from django.db import connections
14+
from django.db import connections, models
1515
from django.db.backends.base.base import NO_DB_ALIAS, BaseDatabaseWrapper
1616
from django.db.backends.utils import CursorDebugWrapper as BaseCursorDebugWrapper
1717
from django.utils.asyncio import async_unsafe
@@ -70,6 +70,15 @@ class DatabaseWrapper(BaseDatabaseWrapper):
7070

7171
def __init__(self, *args, **kwargs):
7272
super().__init__(*args, **kwargs)
73+
74+
def fix_field(value):
75+
return "" if value is None else str(value)
76+
def patched_get_prep_value(self, value):
77+
result = fix_field(value)
78+
return result
79+
models.CharField.get_prep_value = patched_get_prep_value
80+
models.TextField.get_prep_value = patched_get_prep_value
81+
7382

7483
# This dictionary maps Field objects to their associated Gaussdb column
7584
# types, as strings. Column-type strings can contain format strings; they'll

gaussdb_django/operations.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
from functools import lru_cache, partial
3-
from datetime import datetime, date
43
from django.conf import settings
54
from django.db.backends.base.operations import BaseDatabaseOperations
65
from .compiler import InsertUnnest, GaussDBSQLCompiler, SQLInsertCompiler
@@ -15,6 +14,7 @@
1514
from django.db.models.functions import Cast
1615
from django.utils.regex_helper import _lazy_re_compile
1716
from django.db.models import JSONField, IntegerField
17+
from django.db import models
1818

1919

2020
@lru_cache
@@ -28,7 +28,6 @@ class DatabaseOperations(BaseDatabaseOperations):
2828
def __init__(self, *args, **kwargs):
2929
super().__init__(*args, **kwargs)
3030

31-
# compiler_module = "gaussdb_django.compiler"
3231
cast_char_field_without_max_length = "varchar"
3332
explain_prefix = "EXPLAIN"
3433
explain_options = frozenset(
@@ -439,7 +438,6 @@ def converter(value, expression, connection):
439438

440439
return [converter] + converters
441440
if isinstance(expression.output_field, IntegerField):
442-
443441
def int_safe_converter(value, expression, connection):
444442
if value is None:
445443
return None
@@ -451,5 +449,10 @@ def int_safe_converter(value, expression, connection):
451449
return None
452450

453451
return [int_safe_converter] + converters
454-
452+
if isinstance(expression.output_field, (models.CharField, models.TextField)):
453+
def none_to_empty(value, expression, connection):
454+
if value is None:
455+
return ""
456+
return value
457+
converters.append(none_to_empty)
455458
return converters

0 commit comments

Comments
 (0)