Querying database with ormar
QuerySet
Each Model is auto registered with a QuerySet that represents the underlying query,
and it's options.
Most of the methods are also available through many to many relations and on reverse
foreign key relations through QuerysetProxy interface.
Info
To see which relations are supported and how to construct relations visit relations.
For simplicity available methods to fetch and save the data into the database are divided into categories according to the function they fulfill.
Note that some functions/methods are in multiple categories.
For completeness, Model and relation methods are listed.
To read more about any specific section or function please refer to the details subpage.
Insert data into database
create(**kwargs) -> Modelget_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs) -> Tuple[Model, bool]update_or_create(**kwargs) -> Model-
bulk_create(objects: List[Model]) -> None -
ModelModel.save()methodModel.upsert()methodModel.save_related()method
-
QuerysetProxyQuerysetProxy.create(**kwargs)methodQuerysetProxy.get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs)methodQuerysetProxy.update_or_create(**kwargs)method
Tip
To read more about any or all of those functions visit create section.
Read data from database
get(**kwargs) -> Modelget_or_none(**kwargs) -> Optional[Model]get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs) -> Tuple[Model, bool]first() -> Model-
all(**kwargs) -> List[Optional[Model]] -
ModelModel.load()method
-
QuerysetProxyQuerysetProxy.get(**kwargs)methodQuerysetProxy.get_or_none(**kwargs)methodQuerysetProxy.get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs)methodQuerysetProxy.first()methodQuerysetProxy.all(**kwargs)method
Tip
To read more about any or all of those functions visit read section.
Read raw data from database
Instead of ormar models return raw data in form list of dictionaries or tuples.
values(fields = None, exclude_through = False) -> List[Dict]-
values_list(fields = None, exclude_through = False, flatten = False) -> List -
QuerysetProxyQuerysetProxy.values(fields = None, exclude_through = False)methodQuerysetProxy.values_list(fields = None, exclude_through= False, flatten = False)method
Tip
To read more about any or all of those functions visit raw data section.
Update data in database
update(each: bool = False, **kwargs) -> intupdate_or_create(**kwargs) -> Model-
bulk_update(objects: List[Model], columns: List[str] = None) -> None -
ModelModel.update()methodModel.upsert()methodModel.save_related()method
-
QuerysetProxyQuerysetProxy.update_or_create(**kwargs)method
Tip
To read more about any or all of those functions visit update section.
Delete data from database
-
delete(each: bool = False, **kwargs) -> int -
ModelModel.delete()method
-
QuerysetProxyQuerysetProxy.remove()methodQuerysetProxy.clear()method
Tip
To read more about any or all of those functions visit delete section.
Joins and subqueries
select_related(related: Union[List, str]) -> QuerySet-
prefetch_related(related: Union[List, str]) -> QuerySet -
ModelModel.load()method
-
QuerysetProxyQuerysetProxy.select_related(related: Union[List, str])methodQuerysetProxy.prefetch_related(related: Union[List, str])method
Tip
To read more about any or all of those functions visit joins and subqueries section.
Filtering and sorting
filter(**kwargs) -> QuerySetexclude(**kwargs) -> QuerySetorder_by(columns:Union[List, str]) -> QuerySetget(**kwargs) -> Modelget_or_none(**kwargs) -> Optional[Model]get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs) -> Tuple[Model, bool]-
all(**kwargs) -> List[Optional[Model]] -
QuerysetProxyQuerysetProxy.filter(**kwargs)methodQuerysetProxy.exclude(**kwargs)methodQuerysetProxy.order_by(columns:Union[List, str])methodQuerysetProxy.get(**kwargs)methodQuerysetProxy.get_or_none(**kwargs)methodQuerysetProxy.get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs)methodQuerysetProxy.all(**kwargs)method
Tip
To read more about any or all of those functions visit filtering and sorting section.
Selecting columns
fields(columns: Union[List, str, set, dict]) -> QuerySet-
exclude_fields(columns: Union[List, str, set, dict]) -> QuerySet -
QuerysetProxyQuerysetProxy.fields(columns: Union[List, str, set, dict])methodQuerysetProxy.exclude_fields(columns: Union[List, str, set, dict])method
Tip
To read more about any or all of those functions visit selecting columns section.
Pagination and rows number
paginate(page: int) -> QuerySetlimit(limit_count: int) -> QuerySetoffset(offset: int) -> QuerySetget() -> Model-
first() -> Model -
QuerysetProxyQuerysetProxy.paginate(page: int)methodQuerysetProxy.limit(limit_count: int)methodQuerysetProxy.offset(offset: int)method
Tip
To read more about any or all of those functions visit pagination section.
Aggregated functions
count(distinct: bool = True) -> int-
exists() -> bool -
QuerysetProxyQuerysetProxy.count(distinct=True)methodQuerysetProxy.exists()method
Tip
To read more about any or all of those functions visit aggregations section.