100 lines
3.3 KiB
Diff
100 lines
3.3 KiB
Diff
From 68a74fd8f5cc0ce0778bda680b3ca005e68b9530 Mon Sep 17 00:00:00 2001
|
|
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hertzog@debian.org>
|
|
Date: Thu, 18 Aug 2016 14:14:44 +0200
|
|
Subject: Register explicit lookup operators
|
|
|
|
Django 1.10 dropped support for Field.get_prep_lookup() and we need
|
|
to register explicit lookup operators overriding the default operators
|
|
to have our desired behaviour.
|
|
|
|
Fix #56
|
|
|
|
Bug: https://bitbucket.org/schinckel/django-jsonfield/issues/56/unit-test-test_query_object-fails-with
|
|
Bug-Debian: https://bugs.debian.org/828668
|
|
Applied-Upstream: 1.0.2
|
|
---
|
|
jsonfield/fields.py | 59 +++++++++++++++++++++++++++++++++++++++--------------
|
|
1 file changed, 44 insertions(+), 15 deletions(-)
|
|
|
|
diff --git a/jsonfield/fields.py b/jsonfield/fields.py
|
|
index befef2c..dd390ef 100644
|
|
--- a/jsonfield/fields.py
|
|
+++ b/jsonfield/fields.py
|
|
@@ -4,6 +4,7 @@ import json
|
|
from django.core.exceptions import ValidationError
|
|
from django.conf import settings
|
|
from django.db import models
|
|
+from django.db.models.lookups import Exact, IExact, In, Contains, IContains
|
|
from django.db.backends.signals import connection_created
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.utils import six
|
|
@@ -95,25 +96,53 @@ class JSONField(models.Field):
|
|
return None
|
|
return json.dumps(value, **self.encoder_kwargs)
|
|
|
|
- def get_prep_lookup(self, lookup_type, value):
|
|
- if lookup_type in ["exact", "iexact", "in", "isnull"]:
|
|
- return value
|
|
- if lookup_type in ["contains", "icontains"]:
|
|
- if isinstance(value, (list, tuple)):
|
|
- raise TypeError("Lookup type %r not supported with argument of %s" % (
|
|
- lookup_type, type(value).__name__
|
|
- ))
|
|
- # Need a way co combine the values with '%', but don't escape that.
|
|
- return self.get_prep_value(value)[1:-1].replace(', ', r'%')
|
|
- if isinstance(value, dict):
|
|
- return self.get_prep_value(value)[1:-1]
|
|
- return self.get_prep_value(value)
|
|
- raise TypeError('Lookup type %r not supported' % lookup_type)
|
|
-
|
|
def value_to_string(self, obj):
|
|
return self._get_val_from_obj(obj)
|
|
|
|
|
|
+class NoPrepareMixin(object):
|
|
+ def get_prep_lookup(self):
|
|
+ return self.rhs
|
|
+
|
|
+
|
|
+class JSONFieldExactLookup(NoPrepareMixin, Exact):
|
|
+ pass
|
|
+
|
|
+
|
|
+class JSONFieldIExactLookup(NoPrepareMixin, IExact):
|
|
+ pass
|
|
+
|
|
+
|
|
+class JSONFieldInLookup(NoPrepareMixin, In):
|
|
+ pass
|
|
+
|
|
+
|
|
+class ContainsLookupMixin(object):
|
|
+ def get_prep_lookup(self):
|
|
+ if isinstance(self.rhs, (list, tuple)):
|
|
+ raise TypeError("Lookup type %r not supported with %s argument" % (
|
|
+ self.lookup_name, type(self.rhs).__name__
|
|
+ ))
|
|
+ if isinstance(self.rhs, dict):
|
|
+ return self.lhs.output_field.get_prep_value(self.rhs)[1:-1]
|
|
+ return self.lhs.output_field.get_prep_value(self.rhs)
|
|
+
|
|
+
|
|
+class JSONFieldContainsLookup(ContainsLookupMixin, Contains):
|
|
+ pass
|
|
+
|
|
+
|
|
+class JSONFieldIContainsLookup(ContainsLookupMixin, IContains):
|
|
+ pass
|
|
+
|
|
+
|
|
+JSONField.register_lookup(JSONFieldExactLookup)
|
|
+JSONField.register_lookup(JSONFieldIExactLookup)
|
|
+JSONField.register_lookup(JSONFieldInLookup)
|
|
+JSONField.register_lookup(JSONFieldContainsLookup)
|
|
+JSONField.register_lookup(JSONFieldIContainsLookup)
|
|
+
|
|
+
|
|
class TypedJSONField(JSONField):
|
|
"""
|
|
|