Skip to content

Commit 46ef40e

Browse files
committed
Added doc for the CONTAINS operator
1 parent 21b8fac commit 46ef40e

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

cassandra/cqlengine/query.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,9 @@ def filter(self, *args, **kwargs):
557557
query_val = [column.to_database(v) for v in val]
558558
elif isinstance(val, BaseQueryFunction):
559559
query_val = val
560-
elif isinstance(column, (columns.List, columns.Set, columns.Map)):
561-
# For collections, we query using the value, not the container
560+
elif (isinstance(operator, ContainsOperator) and
561+
isinstance(column, (columns.List, columns.Set, columns.Map))):
562+
# For ContainsOperator and collections, we query using the value, not the container
562563
query_val = val
563564
else:
564565
query_val = column.to_database(val)

docs/cqlengine/queryset.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Retrieving objects with filters
3737
year = columns.Integer(primary_key=True)
3838
model = columns.Text()
3939
price = columns.Decimal()
40+
options = columns.Set(columns.Text)
4041
4142
...and assuming the Automobile table contains a record of every car model manufactured in the last 20 years or so, we can retrieve only the cars made by a single manufacturer like this:
4243

@@ -172,6 +173,16 @@ Filtering Operators
172173
173174
q.filter(Automobile.year <= 2012)
174175
176+
:attr:`CONTAINS (__contains) <query.QueryOperator.ContainsOperator>`
177+
178+
The CONTAINS operator is available for all collection types (List, Set, Map).
179+
180+
.. code-block:: python
181+
182+
q = Automobile.objects.filter(manufacturer='Tesla')
183+
q.filter(options__contains='backup camera').allow_filtering()
184+
185+
Note that we need to use allow_filtering() since the *options* column has no secondary index.
175186

176187
TimeUUID Functions
177188
==================
@@ -341,4 +352,3 @@ Named tables are a way of querying a table without creating an class. They're u
341352
user.objects()[0]
342353
343354
# {u'pk': 1, u't': datetime.datetime(2014, 6, 26, 17, 10, 31, 774000)}
344-

0 commit comments

Comments
 (0)