mirror of https://github.com/django/django.git
Add HasAnyKeys lookup for HStoreField.
This commit is contained in:
parent
38eacbde62
commit
74fe4428e5
|
@ -55,18 +55,9 @@ class HStoreField(Field):
|
|||
|
||||
HStoreField.register_lookup(lookups.DataContains)
|
||||
HStoreField.register_lookup(lookups.ContainedBy)
|
||||
|
||||
|
||||
@HStoreField.register_lookup
|
||||
class HasKeyLookup(lookups.PostgresSimpleLookup):
|
||||
lookup_name = 'has_key'
|
||||
operator = '?'
|
||||
|
||||
|
||||
@HStoreField.register_lookup
|
||||
class HasKeysLookup(lookups.PostgresSimpleLookup):
|
||||
lookup_name = 'has_keys'
|
||||
operator = '?&'
|
||||
HStoreField.register_lookup(lookups.HasKey)
|
||||
HStoreField.register_lookup(lookups.HasKeys)
|
||||
HStoreField.register_lookup(lookups.HasAnyKeys)
|
||||
|
||||
|
||||
class KeyTransform(Transform):
|
||||
|
|
|
@ -30,6 +30,21 @@ class Overlap(PostgresSimpleLookup):
|
|||
operator = '&&'
|
||||
|
||||
|
||||
class HasKey(PostgresSimpleLookup):
|
||||
lookup_name = 'has_key'
|
||||
operator = '?'
|
||||
|
||||
|
||||
class HasKeys(PostgresSimpleLookup):
|
||||
lookup_name = 'has_keys'
|
||||
operator = '?&'
|
||||
|
||||
|
||||
class HasAnyKeys(PostgresSimpleLookup):
|
||||
lookup_name = 'has_any_keys'
|
||||
operator = '?|'
|
||||
|
||||
|
||||
class Unaccent(FunctionTransform):
|
||||
bilateral = True
|
||||
lookup_name = 'unaccent'
|
||||
|
|
|
@ -385,6 +385,23 @@ Returns objects where the given key is in the data. Uses the SQL operator
|
|||
>>> Dog.objects.filter(data__has_key='owner')
|
||||
[<Dog: Meg>]
|
||||
|
||||
.. fieldlookup:: hstorefield.has_any_keys
|
||||
|
||||
has_any_keys
|
||||
~~~~~~~~~~~~
|
||||
|
||||
.. versionadded:: 1.9
|
||||
|
||||
Returns objects where any of the given keys are in the data. Uses the SQL
|
||||
operator ``?|``. For example::
|
||||
|
||||
>>> Dog.objects.create(name='Rufus', data={'breed': 'labrador'})
|
||||
>>> Dog.objects.create(name='Meg', data={'owner': 'Bob'})
|
||||
>>> Dog.objects.create(name='Fred', data={})
|
||||
|
||||
>>> Dog.objects.filter(data__has_any_keys=['owner', 'breed'])
|
||||
[<Dog: Rufus>, <Dog: Meg>]
|
||||
|
||||
.. fieldlookup:: hstorefield.has_keys
|
||||
|
||||
has_keys
|
||||
|
|
|
@ -79,6 +79,12 @@ class TestQuerying(PostgresSQLTestCase):
|
|||
self.objs[1:2]
|
||||
)
|
||||
|
||||
def test_has_any_keys(self):
|
||||
self.assertSequenceEqual(
|
||||
HStoreModel.objects.filter(field__has_any_keys=['a', 'c']),
|
||||
self.objs[:3]
|
||||
)
|
||||
|
||||
def test_key_transform(self):
|
||||
self.assertSequenceEqual(
|
||||
HStoreModel.objects.filter(field__a='b'),
|
||||
|
|
Loading…
Reference in New Issue