Skip to content

Commit f6ef633

Browse files
committed
HStoreField should still cast all non-expression values to string
1 parent 780713d commit f6ef633

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

psqlextra/fields/hstore_field.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import List, Tuple, Union
22

3+
from django.db.models.expressions import Expression
4+
from django.db.models.fields import Field
35
from django.contrib.postgres.fields import HStoreField as DjangoHStoreField
46

57
from psqlextra.expressions import HStoreValue
@@ -24,15 +26,29 @@ def __init__(self, *args,
2426
self.uniqueness = uniqueness
2527
self.required = required
2628

27-
def get_db_prep_value(self, value, connection, prepared=False):
29+
def get_prep_value(self, value):
2830
"""Override the base class so it doesn't cast all values
2931
to strings.
3032
3133
psqlextra supports expressions in hstore fields, so casting
3234
all values to strings is a bad idea."""
3335

34-
if not value:
35-
return None
36+
value = Field.get_prep_value(self, value)
37+
38+
if isinstance(value, dict):
39+
prep_value = {}
40+
for key, val in value.items():
41+
if isinstance(val, Expression):
42+
prep_value[key] = val
43+
elif val is not None:
44+
prep_value[key] = str(val)
45+
46+
prep_value[key] = val
47+
48+
value = prep_value
49+
50+
if isinstance(value, list):
51+
value = [str(item) for item in value]
3652

3753
return value
3854

0 commit comments

Comments
 (0)