## QuerySet


Extended queryset with column-oriented access.


Usage

``` python
QuerySet()
```


> **Note: Note**
>
> See source for available aggregate spatial functions if `django.contrib.gis` is configured.


## Methods

| Name | Description |
|----|----|
| [__contains__()](#__contains__) | Return whether value is present using [exists](QuerySet.md#model_values.QuerySet.exists). |
| [__eq__()](#__eq__) | Return [QuerySet](QuerySet.md#model_values.QuerySet) filtered by comparison to given value. |
| [__getitem__()](#__getitem__) | Allow column access by field names, expressions, or [F](F.md#model_values.F) objects. |
| [__iter__()](#__iter__) | Iteration extended to support [group_by](QuerySet.md#model_values.QuerySet.group_by). |
| [__setitem__()](#__setitem__) | Update a single column. |
| [alias()](#alias) | Alias extended to also handle mapping values, as a [Case](Case.md#model_values.Case) expression. |
| [annotate()](#annotate) | Annotate extended to also handle mapping values, as a [Case](Case.md#model_values.Case) expression. |
| [change()](#change) | Update and return number of rows that actually changed. |
| [changed()](#changed) | Return first mapping of fields and values which differ in the db. |
| [exists()](#exists) | Return whether there are at least the specified number of rows. |
| [group_by()](#group_by) | Return a grouped [QuerySet](QuerySet.md#model_values.QuerySet). |
| [reduce()](#reduce) | Return aggregated values, or an annotated [QuerySet](QuerySet.md#model_values.QuerySet). |
| [select()](#select) | Return annotated `values_list`. |
| [sort()](#sort) | Return [QuerySet](QuerySet.md#model_values.QuerySet) ordered by selected values. |
| [update()](#update) | Update extended to also handle mapping values, as a [Case](Case.md#model_values.Case) expression. |
| [value_counts()](#value_counts) | Return annotated value counts. |

------------------------------------------------------------------------


#### \_\_contains\_\_()


Return whether value is present using [exists](QuerySet.md#model_values.QuerySet.exists).


Usage

``` python
__contains__(value)
```


------------------------------------------------------------------------


#### \_\_eq\_\_()


Return [QuerySet](QuerySet.md#model_values.QuerySet) filtered by comparison to given value.


Usage

``` python
__eq__(value, lookup="")
```


------------------------------------------------------------------------


#### \_\_getitem\_\_()


Allow column access by field names, expressions, or [F](F.md#model_values.F) objects.


Usage

``` python
__getitem__(key)
```


`qs[field]` returns flat `values_list`

`qs[field, ...]` returns tupled `values_list`


------------------------------------------------------------------------


#### \_\_iter\_\_()


Iteration extended to support [group_by](QuerySet.md#model_values.QuerySet.group_by).


Usage

``` python
__iter__()
```


------------------------------------------------------------------------


#### \_\_setitem\_\_()


Update a single column.


Usage

``` python
__setitem__(key, value)
```


------------------------------------------------------------------------


#### alias()


Alias extended to also handle mapping values, as a [Case](Case.md#model_values.Case) expression.


Usage

``` python
alias(*args, **kwargs)
```


##### Parameters


`**kwargs`  
`field={Q_obj: value, ...}, ...`


------------------------------------------------------------------------


#### annotate()


Annotate extended to also handle mapping values, as a [Case](Case.md#model_values.Case) expression.


Usage

``` python
annotate(*args, **kwargs)
```


##### Parameters


`**kwargs`  
`field={Q_obj: value, ...}, ...`


As a provisional feature, an optional `default` key may be specified.


------------------------------------------------------------------------


#### change()


Update and return number of rows that actually changed.


Usage

``` python
change(defaults={}, **kwargs)
```


For triggering on-change logic without fetching first.

`if qs.change(status=...):` status actually changed

`qs.change({"last_modified": now}, status=...)` last_modified only updated if status updated


##### Parameters


`defaults: Mapping = {}`\  
optional mapping which will be updated conditionally, as with `update_or_create`.


------------------------------------------------------------------------


#### changed()


Return first mapping of fields and values which differ in the db.


Usage

``` python
changed(**kwargs)
```


Also efficient enough to be used in boolean contexts, instead of [exists](QuerySet.md#model_values.QuerySet.exists).


------------------------------------------------------------------------


#### exists()


Return whether there are at least the specified number of rows.


Usage

``` python
exists(count=1)
```


------------------------------------------------------------------------


#### group_by()


Return a grouped [QuerySet](QuerySet.md#model_values.QuerySet).


Usage

``` python
group_by(*fields, **annotations)
```


The queryset is iterable in the same manner as `itertools.groupby`. Additionally the [reduce](QuerySet.md#model_values.QuerySet.reduce) functions will return annotated querysets.


------------------------------------------------------------------------


#### reduce()


Return aggregated values, or an annotated [QuerySet](QuerySet.md#model_values.QuerySet).


Usage

``` python
reduce(*funcs, **extra)
```


##### Parameters


`*funcs`  
aggregation function classes


------------------------------------------------------------------------


#### select()


Return annotated `values_list`.


Usage

``` python
select(*fields, **annotations)
```


------------------------------------------------------------------------


#### sort()


Return [QuerySet](QuerySet.md#model_values.QuerySet) ordered by selected values.


Usage

``` python
sort(reverse=False)
```


------------------------------------------------------------------------


#### update()


Update extended to also handle mapping values, as a [Case](Case.md#model_values.Case) expression.


Usage

``` python
update(**kwargs)
```


##### Parameters


`**kwargs`  
`field={Q_obj: value, ...}, ...`


------------------------------------------------------------------------


#### value_counts()


Return annotated value counts.


Usage

``` python
value_counts(alias="count")
```
