Last updated
Last updated
Anytime your results return multiple values then an instance of Collection
is returned. This allows you to iterate over your values and has a lot of shorthand methods.
When using collections as a query result you can iterate over it as if the collection with a normal list:
Returns the underlying list or dict represented by the collection:
Returns the average of all items in the collection:
If the collection contains nested objects or dictionaries (e.g. for a collection of models), you must pass a key to use for determining which values to calculate the average:
Chunks a collection into multiple, smaller collections of a given size. Uses a generator to keep each chunk small. Useful for chunking large data sets where pulling too many results in memory will overload the application.
Collapses a collection of lists into a flat collection:
Determines whether the collection contains a given item:
You can also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection.
Finally, you may also pass a callback to the contains method to perform your own truth test:
Returns the total number of items in the collection. len()
standard python method can also be used.
Returns the difference as a collection against another collection
Iterates over the items in the collection and passes each item to a given callback:
Creates a new collection by applying a given callback on every element:
Filters the collection by a given callback, keeping only those items that pass a given truth test:
Returns the first item of the collection, if no arguments are given.
When given a truth test as callback, it returns the first element in the collection that passes the test:
Flattens a multi-dimensional collection into a single dimension:
Removes an item from the collection by its key:
Unlike most other collection methods, forget
does not return a new modified collection; it modifies the collection it is called on.
Paginates the collection by returning a new collection containing the items that would be present on a given page number:
for_page(page, count)
takes the page number and the number of items to show per page.
Returns the item at a given key or index. If the key does not exist, None is returned. An optional default value can be passed as the second argument:
Returns a collection where items are grouped by the given key:
Joins the items in a collection with ,
or the given glue string.
If the collection contains dictionaries or objects, you must pass the key of the attributes you wish to join:
Returns True
if the collection is empty; otherwise, False
is returned:
Returns the last element in the collection if no arguments are given.
Returns the last element in the collection that passes the given truth test:
Iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:
Iterates through the collection and cast each value into the given class:
A class method can also be specified. Some additional keywords arguments can be passed to this method:
Retrieves max value of the collection:
If the collection contains dictionaries or objects, you must pass the key on which to compute max value:
Merges the given list into the collection:
Unlike most other collection methods, merge
does not return a new modified collection; it modifies the collection it is called on.
Retrieves all of the collection values for a given key:
A key can be given to pluck the collection into a dictionary with the given key
You can pass keep_nulls=False
to remove None
value in the collection.
Removes and returns the last item from the collection:
Adds an item to the beginning of the collection:
Removes and returns an item from the collection by its key:
Appends an item to the end of the collection:
Sets the given key and value in the collection:
Returns a random item from the collection
An integer count can be given to random
method to specify how many items you would like to randomly retrieve from the collection. A collection will always be returned when the items count is specified
If the collection length is smaller than specified count a ValueError
will be raised.
Reduces the collection to a single value, passing the result of each iteration into the subsequent iteration.
Initial value is 0
by default but can be overridden:
Unlike most other collection methods, reject
does not return a new modified collection; it modifies the collection it is called on.
Reverses the order of the items in the collection:
Unlike most other collection methods, reverse
does not return a new modified collection; it modifies the collection it is called on.
Removes and returns the first item from the collection:
Sorts the collection:
Returns the sum of all items in the collection:
If the collection contains dictionaries or objects, you must pass a key to use for determining which values to sum:
Returns a new collection with the specified number of items:
You can also pass a negative integer to take the specified amount of items from the end of the collection:
Converts the collection into JSON:
Iterates over the collection and calls the given callback with each item in the collection. The items in the collection will be replaced by the values returned by the callback:
Returns all of the unique items in the collection:
When dealing with dictionaries or objects, you can specify the key used to determine uniqueness:
Filters the collection by a given key / value pair:
Merges together the values of the given list with the values of the collection at the corresponding index:
If you want to transform the original collection, use the method.
It's the inverse of method. It filters the collection using the given callback. The callback should return True
for any items to remove from the resulting collection:
Converts the collection into a list. If the collection’s values are , the models will also be converted to dictionaries:
Be careful, serialize
also converts all of its nested objects. If you want to get the underlying items as is, use the method instead.
If you wish to create a new collection instead, use the method.