Merge pull request #6030 from chrismeyersfsu/fix-6016

add json a=null support and still support a="null"
This commit is contained in:
Chris Meyers
2017-04-16 21:09:58 -04:00
committed by GitHub
2 changed files with 14 additions and 1 deletions

View File

@@ -49,6 +49,11 @@ class JSONField(upstream_JSONField):
class JSONBField(upstream_JSONBField): class JSONBField(upstream_JSONBField):
def get_prep_lookup(self, lookup_type, value):
if isinstance(value, basestring) and value == "null":
return 'null'
return super(JSONBField, self).get_prep_lookup(lookup_type, value)
def get_db_prep_value(self, value, connection, prepared=False): def get_db_prep_value(self, value, connection, prepared=False):
if connection.vendor == 'sqlite': if connection.vendor == 'sqlite':
# sqlite (which we use for tests) does not support jsonb; # sqlite (which we use for tests) does not support jsonb;
@@ -405,7 +410,7 @@ class DynamicFilterField(models.TextField):
Note: we could have totally "ripped" them off earlier when we decided Note: we could have totally "ripped" them off earlier when we decided
what type to convert the token to. what type to convert the token to.
''' '''
if type(v) is unicode and v.startswith('"') and v.endswith('"'): if type(v) is unicode and v.startswith('"') and v.endswith('"') and v != u'"null"':
v = v[1:-1] v = v[1:-1]
if contains_count == 0: if contains_count == 0:

View File

@@ -86,6 +86,14 @@ class TestDynamicFilterFieldFilterStringToQ():
q = DynamicFilterField.filter_string_to_q(filter_string) q = DynamicFilterField.filter_string_to_q(filter_string)
assert unicode(q) == unicode(q_expected) assert unicode(q) == unicode(q_expected)
@pytest.mark.parametrize("filter_string,q_expected", [
('a__b__c=null', Q(**{u"a__b__c": u"null"})),
('a__b__c="null"', Q(**{u"a__b__c": u"\"null\""})),
])
def test_contains_query_generated_null(self, filter_string, q_expected):
q = DynamicFilterField.filter_string_to_q(filter_string)
assert unicode(q) == unicode(q_expected)
''' '''
#('"facts__quoted_val"="f\"oo"', 1), #('"facts__quoted_val"="f\"oo"', 1),