Query database with ormar
query set
Each model is automatically registered to a query set representing the underlying query and its options.
Most methods can also implement reverse foreign key relationships through many-to-many relationships and through the QuerysetProxy interface.
!!!info To see which relationships are supported and how they are structured, visit Relationships.
For simplicity, the available methods for retrieving data and saving it to a database fall into several categories based on the functionality they implement.
Note that some functions/methods fall into more than one category.
For completeness, models and relational methods are listed.
To learn more about any particular section or feature, see the Details subpage.
Insert data into database
-
create (**kwargs) -> model
-
get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs) -> Tuple[Model, bool] -
update_or_create(**kwargs) -> Model -
bulk_create(objects: List[Model]) -> None -
model (作为商品类名时使用复数)
- Model.save () Method
- Model.upsert() method
- Model.save_lated() Method
-
query set proxy
-
QuerysetProxy.create(**kwargs)method QuerysetProxy.get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs)methodQuerysetProxy.update_or_create(**kwargs)method
!!!tip To learn more about any or all of these functions, visit the Creating section.
Read data from database
-
get(**kwargs) -> Model
-
get_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]] -
model (作为商品类名时使用复数)
- Model.load() method
-
query set proxy
-
QuerysetProxy.get(**kwargs) method
QuerysetProxy.get_or_none(**kwargs)methodQuerysetProxy.get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs)method- QuerysetProxy.first() method
- QuerysetProxy.all(**kwargs) method
!!!Tip To learn more about any or all of these features, visit the Reading section.
Read raw data from database
The model returns raw data as a dictionary or tuple list, not an ormar.
-
values(fields = None, exclude_through = False) -> List[Dict] -
values_list(fields = None, exclude_through = False, flatten = False) -> List -
query set proxy
QuerysetProxy.values(fields = None, exclude_through = False)methodQuerysetProxy.values_list(fields = None, exclude_through= False, flatten = False)method
!!!Tip To learn more about any or all of these functions, visit the Raw Data section.
Update data in database
-
update(each: bool = False, **kwargs) -> int -
update_or_create(**kwargs) -> Model -
bulk_update(objects: List[Model], columns: List[str] = None) -> None -
model (作为商品类名时使用复数)
- Model.update() method
- Model.upsert() method
- Model.save_lated() Method
-
query set proxy
-
QuerysetProxy.update_or_create(**kwargs)method
!!!Tip To learn more about any or all of these features, visit the updates section.
Delete data from database
-
delete(each: bool = False, **kwargs) -> int -
model (作为商品类名时使用复数)
- Model.delete() method
-
query set proxy
-
QuerysetProxy.remove() method
- QuerysetProxy.clear() method
!!!Tip To learn more about any or all of these features, visit the Remove section.
Join and subquery
-
select_related(related: Union[List, str]) -> QuerySet -
prefetch_related(related: Union[List, str]) -> QuerySet -
model (作为商品类名时使用复数)
- Model.load() method
-
query set proxy
-
QuerysetProxy.select_related(related: Union[List, str])method QuerysetProxy.prefetch_related(related: Union[List, str])method
!!!Tip To learn more about any or all of these functions, visit the Joins and Subqueries section.
filtering and sorting
-
filter (**kwargs) -> query set
-
exclude (**kwargs) -> query set
-
order_by(columns:Union[List, str]) -> QuerySet -
get(**kwargs) -> Model
-
get_or_none(**kwargs) -> Optional[Model] -
get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs) -> Tuple[Model, bool] -
all(**kwargs) -> List[Optional[Model]] -
query set proxy
QuerysetProxy.filter(**kwargs)methodQuerysetProxy.exclude(**kwargs)methodQuerysetProxy.order_by(columns:Union[List, str])method- QuerysetProxy.get(**kwargs) method
QuerysetProxy.get_or_none(**kwargs)methodQuerysetProxy.get_or_create(_defaults: Optional[Dict[str, Any]] = None, **kwargs)method- QuerysetProxy.all(**kwargs) method
!!!Tip To learn more about any or all of these features, visit the Filtering and Sorting section.
select columns
-
fields(columns: Union[List, str, set, dict]) -> QuerySet -
exclude_fields(columns: Union[List, str, set, dict]) -> QuerySet -
query set proxy
QuerysetProxy.fields(columns: Union[List, str, set, dict])methodQuerysetProxy.exclude_fields(columns: Union[List, str, set, dict])method
!!!tip To learn more about any or all of these features, visit the Select Columns section.
Pages and rows
-
paginate(page: int) -> QuerySet -
limit(limit_count: int) -> QuerySet -
offset(offset: int) -> QuerySet -
get() -> Model
-
First () -> Model
-
query set proxy
QuerysetProxy.paginate(page: int)methodQuerysetProxy.limit(limit_count: int)methodQuerysetProxy.offset(offset: int)method
!!!Tip To learn more about any or all of these features, visit the pagination section.
aggregate function
-
count(distinct: bool = True) -> int -
Existence () -> Boolean
-
query set proxy
QuerysetProxy.count(distinct=True)method- QuerysetProxy.exists() method
!!!tip To learn more about any or all of these functions, visit the Aggregations section.