API Reference¶
Models¶
-
class
Model
(**kwargs)¶ Models provide a 1-to-1 mapping to database tables. Subclasses of
Model
declare any number ofField
instances as class attributes. These fields correspond to columns on the table.Table-level operations, such as
select()
,update()
,insert()
, anddelete()
, are implemented as classmethods. Row-level operations such assave()
anddelete_instance()
are implemented as instancemethods.Parameters: kwargs – Initialize the model, assigning the given key/values to the appropriate fields. Example:
class User(Model): username = CharField() join_date = DateTimeField(default=datetime.datetime.now) is_admin = BooleanField() u = User(username='charlie', is_admin=True)
-
classmethod
select
(*selection)¶ Parameters: selection – A list of model classes, field instances, functions or expressions. If no argument is provided, all columns for the given model will be selected. Return type: a SelectQuery
for the givenModel
.Examples of selecting all columns (default):
User.select().where(User.active == True).order_by(User.username)
Example of selecting all columns on Tweet and the parent model, User. When the
user
foreign key is accessed on a Tweet instance no additional query will be needed (see N+1 for more details):(Tweet .select(Tweet, User) .join(User) .order_by(Tweet.created_date.desc()))
-
classmethod
update
(**update)¶ Parameters: update – mapping of field-name to expression Return type: an UpdateQuery
for the givenModel
Example showing users being marked inactive if their registration expired:
q = User.update(active=False).where(User.registration_expired == True) q.execute() # Execute the query, updating the database.
Example showing an atomic update:
q = PageView.update(count=PageView.count + 1).where(PageView.url == url) q.execute() # execute the query, updating the database.
Note
When an update query is executed, the number of rows modified will be returned.
-
classmethod
insert
(**insert)¶ Insert a new row into the database. If any fields on the model have default values, these values will be used if the fields are not explicitly set in the
insert
dictionary.Parameters: insert – mapping of field or field-name to expression. Return type: an InsertQuery
for the givenModel
.Example showing creation of a new user:
q = User.insert(username='admin', active=True, registration_expired=False) q.execute() # perform the insert.
You can also use
Field
objects as the keys:User.insert(**{User.username: 'admin'}).execute()
If you have a model with a default value on one of the fields, and that field is not specified in the
insert
parameter, the default will be used:class User(Model): username = CharField() active = BooleanField(default=True) # This INSERT query will automatically specify `active=True`: User.insert(username='charlie')
Note
When an insert query is executed on a table with an auto-incrementing primary key, the primary key of the new row will be returned.
-
insert_many
(rows)¶ Insert multiple rows at once. The
rows
parameter must be an iterable that yields dictionaries. As withinsert()
, fields that are not specified in the dictionary will use their default value, if one exists.Note
Due to the nature of bulk inserts, each row must contain the same fields. The following will not work:
Person.insert_many([ {'first_name': 'Peewee', 'last_name': 'Herman'}, {'first_name': 'Huey'}, # Missing "last_name"! ])
Parameters: rows – An iterable containing dictionaries of field-name-to-value. Return type: an InsertQuery
for the givenModel
.Example of inserting multiple Users:
usernames = ['charlie', 'huey', 'peewee', 'mickey'] row_dicts = ({'username': username} for username in usernames) # Insert 4 new rows. User.insert_many(row_dicts).execute()
Because the
rows
parameter can be an arbitrary iterable, you can also use a generator:def get_usernames(): for username in ['charlie', 'huey', 'peewee']: yield {'username': username} User.insert_many(get_usernames()).execute()
-
classmethod
insert_from
(fields, query)¶ Insert rows into the table using a query as the data source. This API should be used for INSERT INTO...SELECT FROM queries.
Parameters: - fields – The field objects to map the selected data into.
- query – The source of the new rows.
Return type: an
InsertQuery
for the givenModel
.Example of inserting data across tables for denormalization purposes:
source = (User .select(User.username, fn.COUNT(Tweet.id)) .join(Tweet, JOIN_LEFT_OUTER) .group_by(User.username)) UserTweetDenorm.insert_from( [UserTweetDenorm.username, UserTweetDenorm.num_tweets], source).execute()
-
classmethod
delete
()¶ Return type: a DeleteQuery
for the givenModel
.Example showing the deletion of all inactive users:
q = User.delete().where(User.active == False) q.execute() # remove the rows
Warning
This method performs a delete on the entire table. To delete a single instance, see
Model.delete_instance()
.
-
classmethod
raw
(sql, *params)¶ Parameters: - sql – a string SQL expression
- params – any number of parameters to interpolate
Return type: a
RawQuery
for the givenModel
Example selecting rows from the User table:
q = User.raw('select id, username from users') for user in q: print user.id, user.username
Note
Generally the use of
raw
is reserved for those cases where you can significantly optimize a select query. It is useful for select queries since it will return instances of the model.
-
classmethod
create
(**attributes)¶ Parameters: attributes – key/value pairs of model attributes Return type: a model instance with the provided attributes Example showing the creation of a user (a row will be added to the database):
user = User.create(username='admin', password='test')
Note
The create() method is a shorthand for instantiate-then-save.
-
classmethod
get
(*args)¶ Parameters: args – a list of query expressions, e.g. User.username == 'foo'
Return type: Model
instance or raisesDoesNotExist
exceptionGet a single row from the database that matches the given query. Raises a
<model-class>.DoesNotExist
if no rows are returned:user = User.get(User.username == username, User.password == password)
This method is also exposed via the
SelectQuery
, though it takes no parameters:active = User.select().where(User.active == True) try: user = active.where( (User.username == username) & (User.password == password) ).get() except User.DoesNotExist: user = None
Note
The
get()
method is shorthand for selecting with a limit of 1. It has the added behavior of raising an exception when no matching row is found. If more than one row is found, the first row returned by the database cursor will be used.
-
classmethod
alias
()¶ Return type: ModelAlias
instanceThe
alias()
method is used to create self-joins.Example:
Parent = Category.alias() sq = (Category .select(Category, Parent) .join(Parent, on=(Category.parent == Parent.id)) .where(Parent.name == 'parent category'))
Note
When using a
ModelAlias
in a join, you must explicitly specify the join condition.
-
classmethod
create_table
([fail_silently=False])¶ Parameters: fail_silently (bool) – If set to True
, the method will check for the existence of the table before attempting to create.Create the table for the given model.
Example:
database.connect() SomeModel.create_table() # Execute the create table query.
-
classmethod
drop_table
([fail_silently=False[, cascade=False]])¶ Parameters: - fail_silently (bool) – If set to
True
, the query will check for the existence of the table before attempting to remove. - cascade (bool) – Drop table with
CASCADE
option.
Drop the table for the given model.
- fail_silently (bool) – If set to
-
classmethod
table_exists
()¶ Return type: Boolean whether the table for this model exists in the database
-
classmethod
sqlall
()¶ Returns: A list of queries required to create the table and indexes.
-
save
([force_insert=False[, only=None]])¶ Parameters: - force_insert (bool) – Whether to force execution of an insert
- only (list) – A list of fields to persist – when supplied, only the given fields will be persisted.
Save the given instance, creating or updating depending on whether it has a primary key. If
force_insert=True
an INSERT will be issued regardless of whether or not the primary key exists.Example showing saving a model instance:
user = User() user.username = 'some-user' # does not touch the database user.save() # change is persisted to the db
-
delete_instance
([recursive=False[, delete_nullable=False]])¶ Parameters: - recursive – Delete this instance and anything that depends on it, optionally updating those that have nullable dependencies
- delete_nullable – If doing a recursive delete, delete all dependent objects regardless of whether it could be updated to NULL
Delete the given instance. Any foreign keys set to cascade on delete will be deleted automatically. For more programmatic control, you can call with recursive=True, which will delete any non-nullable related models (those that are nullable will be set to NULL). If you wish to delete all dependencies regardless of whether they are nullable, set
delete_nullable=True
.example:
some_obj.delete_instance() # it is gone forever
-
dependencies
([search_nullable=False])¶ Parameters: search_nullable (bool) – Search models related via a nullable foreign key Return type: Generator expression yielding queries and foreign key fields Generate a list of queries of dependent models. Yields a 2-tuple containing the query and corresponding foreign key field. Useful for searching dependencies of a model, i.e. things that would be orphaned in the event of a delete.
-
dirty_fields
¶ Return a list of fields that were manually set.
Return type: list Note
If you just want to persist modified fields, you can call
model.save(only=model.dirty_fields)
.
-
is_dirty
()¶ Return whether any fields were manually set.
Return type: bool
-
prepared
()¶ This method provides a hook for performing model initialization after the row data has been populated.
-
classmethod
Fields¶
-
Field(null=False, index=False, unique=False, verbose_name=None, help_text=None, db_column=None, default=None, choices=None, primary_key=False, sequence=None, constraints=None, schema=None, **kwargs):
The base class from which all other field types extend.
Parameters: - null (bool) – whether this column can accept
None
orNULL
values - index (bool) – whether to create an index for this column when creating the table
- unique (bool) – whether to create a unique index for this column when creating the table
- verbose_name (string) – specify a “verbose name” for this field, useful for metadata purposes
- help_text (string) – specify some instruction text for the usage/meaning of this field
- db_column (string) – column name to use for underlying storage, useful for compatibility with legacy databases
- default – a value to use as an uninitialized default
- choices – an iterable of 2-tuples mapping
value
todisplay
- primary_key (bool) – whether to use this as the primary key for the table
- sequence (string) – name of sequence (if backend supports it)
- constraints (list) – a list of constraints, e.g.
[Check('price > 0')]
. - schema (string) – name of schema (if backend supports it)
- kwargs – named attributes containing values that may pertain to specific field subclasses, such as “max_length” or “decimal_places”
-
db_field = '<some field type>'
Attribute used to map this field to a column type, e.g. “string” or “datetime”
-
_is_bound
¶ Boolean flag indicating if the field is attached to a model class.
-
model_class
¶ The model the field belongs to. Only applies to bound fields.
-
name
¶ The name of the field. Only applies to bound fields.
-
db_value
(value)¶ Parameters: value – python data type to prep for storage in the database Return type: converted python datatype
-
python_value
(value)¶ Parameters: value – data coming from the backend storage Return type: python data type
-
coerce
(value)¶ This method is a shorthand that is used, by default, by both
db_value
andpython_value
. You can usually get away with just implementing this.Parameters: value – arbitrary data from app or backend Return type: python data type
- null (bool) – whether this column can accept
-
class
IntegerField
¶ Stores: integers
-
db_field = 'int'
-
-
class
BigIntegerField
¶ Stores: big integers
-
db_field = 'bigint'
-
-
class
PrimaryKeyField
¶ Stores: auto-incrementing integer fields suitable for use as primary key.
-
db_field = 'primary_key'
-
-
class
FloatField
¶ Stores: floating-point numbers
-
db_field = 'float'
-
-
class
DoubleField
¶ Stores: double-precision floating-point numbers
-
db_field = 'double'
-
-
class
DecimalField
¶ Stores: decimal numbers, using python standard library
Decimal
objectsAdditional attributes and values:
max_digits
10
decimal_places
5
auto_round
False
rounding
decimal.DefaultContext.rounding
-
db_field = 'decimal'
-
-
class
CharField
¶ Stores: small strings (0-255 bytes)
Additional attributes and values:
max_length
255
-
db_field = 'string'
-
-
class
TextField
¶ Stores: arbitrarily large strings
-
db_field = 'text'
-
-
class
DateTimeField
¶ Stores: python
datetime.datetime
instancesAccepts a special parameter
formats
, which contains a list of formats the datetime can be encoded with. The default behavior is:'%Y-%m-%d %H:%M:%S.%f' # year-month-day hour-minute-second.microsecond '%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second '%Y-%m-%d' # year-month-day
Note
If the incoming value does not match a format, it will be returned as-is
-
db_field = 'datetime'
-
year
¶ An expression suitable for extracting the year, for example to retrieve all blog posts from 2013:
Blog.select().where(Blog.pub_date.year == 2013)
-
month
¶ An expression suitable for extracting the month from a stored date.
-
day
¶ An expression suitable for extracting the day from a stored date.
-
hour
¶ An expression suitable for extracting the hour from a stored time.
-
minute
¶ An expression suitable for extracting the minute from a stored time.
-
second
¶ An expression suitable for extracting the second from a stored time.
-
-
class
DateField
¶ Stores: python
datetime.date
instancesAccepts a special parameter
formats
, which contains a list of formats the date can be encoded with. The default behavior is:'%Y-%m-%d' # year-month-day '%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second '%Y-%m-%d %H:%M:%S.%f' # year-month-day hour-minute-second.microsecond
Note
If the incoming value does not match a format, it will be returned as-is
-
db_field = 'date'
-
year
¶ An expression suitable for extracting the year, for example to retrieve all people born in 1980:
Person.select().where(Person.dob.year == 1983)
-
-
class
TimeField
¶ Stores: python
datetime.time
instancesAccepts a special parameter
formats
, which contains a list of formats the time can be encoded with. The default behavior is:'%H:%M:%S.%f' # hour:minute:second.microsecond '%H:%M:%S' # hour:minute:second '%H:%M' # hour:minute '%Y-%m-%d %H:%M:%S.%f' # year-month-day hour-minute-second.microsecond '%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second
Note
If the incoming value does not match a format, it will be returned as-is
-
db_field = 'time'
-
hour
¶ Extract the hour from a time, for example to retreive all events occurring in the evening:
Event.select().where(Event.time.hour > 17)
-
-
class
BooleanField
¶ Stores:
True
/False
-
db_field = 'bool'
-
-
class
BlobField
¶ Store arbitrary binary data.
-
class
UUIDField
¶ Store
UUID
values. Currently only supported byPostgresqlDatabase
.
-
class
ForeignKeyField
(rel_model[, related_name=None[, on_delete=None[, on_update=None[, to_field=None[, ...]]]]])¶ Stores: relationship to another model
Parameters: - rel_model – related
Model
class or the string ‘self’ if declaring a self-referential foreign key - related_name (string) – attribute to expose on related model
- on_delete (string) – on delete behavior, e.g.
on_delete='CASCADE'
. - on_update (string) – on update behavior.
- to_field – the field (or field name) on
rel_model
the foreign key references. Defaults to the primary key field forrel_model
.
class User(Model): name = CharField() class Tweet(Model): user = ForeignKeyField(User, related_name='tweets') content = TextField() # "user" attribute >>> some_tweet.user <User: charlie> # "tweets" related name attribute >>> for tweet in charlie.tweets: ... print tweet.content Some tweet Another tweet Yet another tweet
Note
Foreign keys do not have a particular
db_field
as they will take their field type depending on the type of primary key on the model they are related to.Note
If you manually specify a
to_field
, that field must be either a primary key or have a unique constraint.- rel_model – related
-
class
CompositeKey
(*fields)¶ Specify a composite primary key for a model. Unlike the other fields, a composite key is defined in the model’s
Meta
class after the fields have been defined. It takes as parameters the string names of the fields to use as the primary key:class BlogTagThrough(Model): blog = ForeignKeyField(Blog, related_name='tags') tag = ForeignKeyField(Tag, related_name='blogs') class Meta: primary_key = CompositeKey('blog', 'tag')
Query Types¶
-
class
Query
¶ The parent class from which all other query classes are drived. While you will not deal with
Query
directly in your code, it implements some methods that are common across all query types.-
where
(*expressions)¶ Parameters: expressions – a list of one or more expressions Return type: a Query
instanceExample selection users where the username is equal to ‘somebody’:
sq = SelectQuery(User).where(User.username == 'somebody')
Example selecting tweets made by users who are either editors or administrators:
sq = SelectQuery(Tweet).join(User).where( (User.is_editor == True) | (User.is_admin == True))
Example of deleting tweets by users who are no longer active:
dq = DeleteQuery(Tweet).where( Tweet.user << User.select().where(User.active == False)) dq.execute() # perform the delete query
Note
where()
calls are chainable. Multiple calls will be “AND”-ed together.
-
join
(model, join_type=None, on=None)¶ Parameters: - model – the model to join on. there must be a
ForeignKeyField
between the currentquery context
and the model passed in. - join_type – allows the type of
JOIN
used to be specified explicitly, one ofJOIN_INNER
,JOIN_LEFT_OUTER
,JOIN_FULL
- on – if multiple foreign keys exist between two models, this parameter is the ForeignKeyField to join on.
Return type: a
Query
instanceGenerate a
JOIN
clause from the currentquery context
to themodel
passed in, and establishesmodel
as the newquery context
.Example selecting tweets and joining on user in order to restrict to only those tweets made by “admin” users:
sq = SelectQuery(Tweet).join(User).where(User.is_admin == True)
Example selecting users and joining on a particular foreign key field. See the example app for a real-life usage:
sq = SelectQuery(User).join(Relationship, on=Relationship.to_user)
- model – the model to join on. there must be a
-
switch
(model)¶ Parameters: model – model to switch the query context
to.Return type: a clone of the query with a new query context Switches the
query context
to the given model. Raises an exception if the model has not been selected or joined on previously. Useful for performing multiple joins from a single table.The following example selects from blog and joins on both entry and user:
sq = SelectQuery(Blog).join(Entry).switch(Blog).join(User)
-
alias
(alias=None)¶ Parameters: alias (str) – A string to alias the result of this query Return type: a Query instance Assign an alias to given query, which can be used as part of a subquery.
-
sql
()¶ Return type: a 2-tuple containing the appropriate SQL query and a tuple of parameters
-
execute
()¶ Execute the given query
-
scalar
([as_tuple=False])¶ Parameters: as_tuple (bool) – return the row as a tuple or a single value Return type: the resulting row, either as a single value or tuple Provide a way to retrieve single values from select queries, for instance when performing an aggregation.
>>> PageView.select(fn.Count(fn.Distinct(PageView.url))).scalar() 100 # <-- there are 100 distinct URLs in the pageview table
-
-
class
SelectQuery
(model_class, *selection)¶ By far the most complex of the query classes available in peewee. It supports all clauses commonly associated with select queries.
Methods on the select query can be chained together.
SelectQuery
implements an__iter__()
method, allowing it to be iterated to return model instances.Parameters: - model – a
Model
class to perform query on - selection – a list of models, fields, functions or expressions
If no selection is provided, it will default to all the fields of the given model.
Example selecting some user instances from the database. Only the
id
andusername
columns are selected. When iterated, will return instances of theUser
model:sq = SelectQuery(User, User.id, User.username) for user in sq: print user.username
Example selecting users and additionally the number of tweets made by the user. The
User
instances returned will have an additional attribute, ‘count’, that corresponds to the number of tweets made:sq = (SelectQuery( User, User, fn.Count(Tweet.id).alias('count')) .join(Tweet) .group_by(User))
-
select
(*selection)¶ Parameters: selection – a list of expressions, which can be model classes or fields. if left blank, will default to all the fields of the given model. Return type: SelectQuery
Note
Usually the selection will be specified when the instance is created. This method simply exists for the case when you want to modify the SELECT clause independent of instantiating a query.
query = User.select() query = query.select(User.username)
-
from_
(*args)¶ Parameters: args – one or more expressions, for example Model
orSelectQuery
instance(s). if left blank, will default to the table of the given model.Return type: SelectQuery
# rather than a join, select from both tables and join with where. query = User.select().from_(User, Blog).where(Blog.user == User.id)
-
group_by
(*clauses)¶ Parameters: clauses – a list of expressions, which can be model classes or individual field instances Return type: SelectQuery
Group by one or more columns. If a model class is provided, all the fields on that model class will be used.
Example selecting users, joining on tweets, and grouping by the user so a count of tweets can be calculated for each user:
sq = (User .select(User, fn.Count(Tweet.id).alias('count')) .join(Tweet) .group_by(User))
-
having
(*expressions)¶ Parameters: expressions – a list of one or more expressions Return type: SelectQuery
Here is the above example selecting users and tweet counts, but restricting the results to those users who have created 100 or more tweets:
sq = (User .select(User, fn.Count(Tweet.id).alias('count')) .join(Tweet) .group_by(User) .having(fn.Count(Tweet.id) > 100))
-
order_by
(*clauses)¶ Parameters: clauses – a list of fields, calls to field.[asc|desc]()
or one or more expressionsReturn type: SelectQuery
Example of ordering users by username:
User.select().order_by(User.username)
Example of selecting tweets and ordering them first by user, then newest first:
Tweet.select().join(User).order_by( User.username, Tweet.created_date.desc())
A more complex example ordering users by the number of tweets made (greatest to least), then ordered by username in the event of a tie:
tweet_ct = fn.Count(Tweet.id) sq = (User .select(User, tweet_ct.alias('count')) .join(Tweet) .group_by(User) .order_by(tweet_ct.desc(), User.username))
-
window
(*windows)¶ Parameters: windows (Window) – One or more Window
instances.Add one or more window definitions to this query.
window = Window(partition_by=[fn.date_trunc('day', PageView.timestamp)]) query = (PageView .select( PageView.url, PageView.timestamp, fn.Count(PageView.id).over(window=window)) .window(window) .order_by(PageView.timestamp))
-
limit
(num)¶ Parameters: num (int) – limit results to num
rows
-
offset
(num)¶ Parameters: num (int) – offset results by num
rows
-
paginate
(page_num, paginate_by=20)¶ Parameters: - page_num – a 1-based page number to use for paginating results
- paginate_by – number of results to return per-page
Return type: Shorthand for applying a
LIMIT
andOFFSET
to the query.Page indices are 1-based, so page 1 is the first page.
User.select().order_by(User.username).paginate(3, 20) # get users 41-60
-
distinct
([is_distinct=True])¶ Parameters: is_distinct – See notes. Return type: SelectQuery
Indicates that this query should only return distinct rows. Results in a
SELECT DISTINCT
query.Note
The value for
is_distinct
should either be a boolean, in which case the query will (or won’t) be DISTINCT.You can specify a list of one or more expressions to generate a
DISTINCT ON
query, e.g..distinct([Model.col1, Model.col2])
.
-
for_update
([for_update=True[, nowait=False]])¶ Return type: SelectQuery
Indicate that this query should lock rows for update. If
nowait
isTrue
then the database will raise anOperationalError
if it cannot obtain the lock.
-
naive
()¶ Return type: SelectQuery
Flag this query indicating it should only attempt to reconstruct a single model instance for every row returned by the cursor. If multiple tables were queried, the columns returned are patched directly onto the single model instance.
Generally this method is useful for speeding up the time needed to construct model instances given a database cursor.
Note
this can provide a significant speed improvement when doing simple iteration over a large result set.
-
iterator
()¶ Return type: iterable
By default peewee will cache rows returned by the cursor. This is to prevent things like multiple iterations, slicing and indexing from triggering extra queries. When you are iterating over a large number of rows, however, this cache can take up a lot of memory. Using
iterator()
will save memory by not storing all the returned model instances.# iterate over large number of rows. for obj in Stats.select().iterator(): # do something. pass
-
tuples
()¶ Return type: SelectQuery
Flag this query indicating it should simply return raw tuples from the cursor. This method is useful when you either do not want or do not need full model instances.
-
dicts
()¶ Return type: SelectQuery
Flag this query indicating it should simply return dictionaries from the cursor. This method is useful when you either do not want or do not need full model instances.
-
aggregate_rows
()¶ Return type: SelectQuery
This method provides one way to avoid the N+1 query problem.
Consider a webpage where you wish to display a list of users and all of their associated tweets. You could approach this problem by listing the users, then for each user executing a separate query to retrieve their tweets. This is the N+1 behavior, because the number of queries varies depending on the number of users. Conventional wisdom is that it is preferable to execute fewer queries. Peewee provides several ways to avoid this problem.
You can use the
prefetch()
helper, which usesIN
clauses to retrieve the tweets for the listed users.Another method is to select both the user and the tweet data in a single query, then de-dupe the users, aggregating the tweets in the process.
The raw column data might appear like this:
# user.id, user.username, tweet.id, tweet.user_id, tweet.message [1, 'charlie', 1, 1, 'hello'], [1, 'charlie', 2, 1, 'goodbye'], [2, 'no-tweets', NULL, NULL, NULL], [3, 'huey', 3, 3, 'meow'], [3, 'huey', 4, 3, 'purr'], [3, 'huey', 5, 3, 'hiss'],
We can infer from the
JOIN
clause that the user data will be duplicated, and therefore by de-duping the users, we can collect their tweets in one go and iterate over the users and tweets transparently.query = (User .select(User, Tweet) .join(Tweet, JOIN_LEFT_OUTER) .order_by(User.username, Tweet.id) .aggregate_rows()) # .aggregate_rows() tells peewee to de-dupe the rows. for user in query: print user.username for tweet in user.tweets: print ' ', tweet.message # Producing the following output: charlie hello goodbye huey meow purr hiss no-tweets
Warning
Be sure that you specify an
ORDER BY
clause that ensures duplicated data will appear in consecutive rows.Note
You can specify arbitrarily complex joins, though for more complex queries it may be more efficient to use
prefetch()
. In short, try both and see what works best for your data-set.Note
For more information, see the Avoiding N+1 queries document.
-
annotate
(related_model, aggregation=None)¶ Parameters: - related_model – related
Model
on which to perform aggregation, must be linked byForeignKeyField
. - aggregation – the type of aggregation to use, e.g.
fn.Count(Tweet.id).alias('count')
Return type: Annotate a query with an aggregation performed on a related model, for example, “get a list of users with the number of tweets for each”:
>>> User.select().annotate(Tweet)
If
aggregation
is None, it will default tofn.Count(related_model.id).alias('count')
but can be anything:>>> user_latest = User.select().annotate(Tweet, fn.Max(Tweet.created_date).alias('latest'))
Note
If the
ForeignKeyField
isnullable
, then aLEFT OUTER
join may need to be used:User.select().join(Tweet, JOIN_LEFT_OUTER).annotate(Tweet)
- related_model – related
-
aggregate
(aggregation)¶ Parameters: aggregation – a function specifying what aggregation to perform, for example fn.Max(Tweet.created_date)
.Method to look at an aggregate of rows using a given function and return a scalar value, such as the count of all rows or the average value of a particular column.
-
count
([clear_limit=False])¶ Parameters: clear_limit (bool) – Remove any limit or offset clauses from the query before counting. Return type: an integer representing the number of rows in the current query Note
If the query has a GROUP BY, DISTINCT, LIMIT, or OFFSET clause, then the
wrapped_count()
method will be used instead.>>> sq = SelectQuery(Tweet) >>> sq.count() 45 # number of tweets >>> deleted_tweets = sq.where(Tweet.status == DELETED) >>> deleted_tweets.count() 3 # number of tweets that are marked as deleted
-
wrapped_count
([clear_limit=True])¶ Parameters: clear_limit (bool) – Remove any limit or offset clauses from the query before counting. Return type: an integer representing the number of rows in the current query Wrap the count query in a subquery. Additional overhead but will give correct counts when performing
DISTINCT
queries or those withGROUP BY
clauses.Note
count()
will automatically default towrapped_count()
in the event the query is distinct or has a grouping.
-
exists
()¶ Return type: boolean whether the current query will return any rows. uses an optimized lookup, so use this rather than get()
.sq = User.select().where(User.active == True) if sq.where(User.username == username, User.password == password).exists(): authenticated = True
-
get
()¶ Return type: Model
instance or raisesDoesNotExist
exceptionGet a single row from the database that matches the given query. Raises a
<model-class>.DoesNotExist
if no rows are returned:active = User.select().where(User.active == True) try: user = active.where(User.username == username).get() except User.DoesNotExist: user = None
This method is also exposed via the
Model
api, in which case it accepts arguments that are translated to the where clause:user = User.get(User.active == True, User.username == username)
-
first
()¶ Return type: Model
instance orNone
if no resultsFetch the first row from a query. The result will be cached in case the entire query result-set should be iterated later.
-
execute
()¶ Return type: QueryResultWrapper
Executes the query and returns a
QueryResultWrapper
for iterating over the result set. The results are managed internally by the query and whenever a clause is added that would possibly alter the result set, the query is marked for re-execution.
-
__iter__
()¶ Executes the query and returns populated model instances:
for user in User.select().where(User.active == True): print user.username
-
__getitem__
(value)¶ Parameters: value – Either an index or a slice
object.Return the model instance(s) at the requested indices. To get the first model, for instance:
query = User.select().order_by(User.username) first_user = query[0] first_five = query[:5]
-
__or__
(rhs)¶ Parameters: rhs – Either a SelectQuery
or aCompoundSelect
Return type: CompoundSelect
Create a
UNION
query with the right-hand object. The result will contain all values from both the left and right queries.customers = Customer.select(Customer.city).where(Customer.state == 'KS') stores = Store.select(Store.city).where(Store.state == 'KS') # Get all cities in kansas where we have either a customer or a store. all_cities = (customers | stores).order_by(SQL('city'))
-
__and__
(rhs)¶ Parameters: rhs – Either a SelectQuery
or aCompoundSelect
Return type: CompoundSelect
Create an
INTERSECT
query. The result will contain values that are in both the left and right queries.customers = Customer.select(Customer.city).where(Customer.state == 'KS') stores = Store.select(Store.city).where(Store.state == 'KS') # Get all cities in kanasas where we have both customers and stores. cities = (customers & stores).order_by(SQL('city'))
-
__sub__
(rhs)¶ Parameters: rhs – Either a SelectQuery
or aCompoundSelect
Return type: CompoundSelect
Create an
EXCEPT
query. The result will contain values that are in the left-hand query but not in the right-hand query.customers = Customer.select(Customer.city).where(Customer.state == 'KS') stores = Store.select(Store.city).where(Store.state == 'KS') # Get all cities in kanasas where we have customers but no stores. cities = (customers - stores).order_by(SQL('city'))
-
__xor__
(rhs)¶ Parameters: rhs – Either a SelectQuery
or aCompoundSelect
Return type: CompoundSelect
Create an symmetric difference query. The result will contain values that are in either the left-hand query or the right-hand query, but not both.
customers = Customer.select(Customer.city).where(Customer.state == 'KS') stores = Store.select(Store.city).where(Store.state == 'KS') # Get all cities in kanasas where we have either customers with no # store, or a store with no customers. cities = (customers ^ stores).order_by(SQL('city'))
- model – a
-
class
UpdateQuery
(model_class, **kwargs)¶ Parameters: - model –
Model
class on which to perform update - kwargs – mapping of field/value pairs containing columns and values to update
Example in which users are marked inactive if their registration expired:
uq = UpdateQuery(User, active=False).where(User.registration_expired == True) uq.execute() # Perform the actual update
Example of an atomic update:
atomic_update = UpdateQuery(PageCount, count = PageCount.count + 1).where( PageCount.url == url) atomic_update.execute() # will perform the actual update
-
execute
()¶ Return type: Number of rows updated Performs the query
- model –
-
class
InsertQuery
(model_class[, field_dict=None[, rows=None[, fields=None[, query=None]]]])¶ Creates an
InsertQuery
instance for the given model.Parameters: - field_dict (dict) – A mapping of either field or field-name to value.
- rows (iterable) – An iterable of dictionaries containing a mapping of field or field-name to value.
- fields (list) – A list of field objects to insert data into (only used in combination with the
query
parameter). - query – A
SelectQuery
to use as the source of data.
Basic example:
>>> fields = {'username': 'admin', 'password': 'test', 'active': True} >>> iq = InsertQuery(User, fields) >>> iq.execute() # insert new row and return primary key 2L
Example inserting multiple rows:
users = [ {'username': 'charlie', 'active': True}, {'username': 'peewee', 'active': False}, {'username': 'huey', 'active': True}] iq = InsertQuery(User, rows=users) iq.execute()
Example inserting using a query as the data source:
query = (User .select(User.username, fn.COUNT(Tweet.id)) .join(Tweet, JOIN_LEFT_OUTER) .group_by(User.username)) iq = InsertQuery( UserTweetDenorm, fields=[UserTweetDenorm.username, UserTweetDenorm.num_tweets], query=query) iq.execute()
-
execute
()¶ Return type: primary key of the new row Performs the query
-
upsert
([upsert=True])¶ Perform an INSERT OR REPLACE query. Currently only Sqlite supports this method.
-
class
DeleteQuery
(model_class)¶ Creates a DELETE query for the given model.
Note
DeleteQuery will not traverse foreign keys or ensure that constraints are obeyed, so use it with care.
Example deleting users whose account is inactive:
dq = DeleteQuery(User).where(User.active == False)
-
execute
()¶ Return type: Number of rows deleted Performs the query
-
-
class
RawQuery
(model_class, sql, *params)¶ Allows execution of an arbitrary query and returns instances of the model via a
QueryResultsWrapper
.Note
Generally you will only need this for executing highly optimized SELECT queries.
Warning
If you are executing a parameterized query, you must use the correct interpolation string for your database. SQLite uses
'?'
and most others use'%s'
.Example selecting users with a given username:
>>> rq = RawQuery(User, 'SELECT * FROM users WHERE username = ?', 'admin') >>> for obj in rq.execute(): ... print obj <User: admin>
-
tuples
()¶ Return type: RawQuery
Flag this query indicating it should simply return raw tuples from the cursor. This method is useful when you either do not want or do not need full model instances.
-
dicts
()¶ Return type: RawQuery
Flag this query indicating it should simply return raw dicts from the cursor. This method is useful when you either do not want or do not need full model instances.
-
execute
()¶ Return type: a QueryResultWrapper
for iterating over the result set. The results are instances of the given model.Performs the query
-
-
class
CompoundSelect
(model_class, lhs, operator, rhs)¶ Compound select query.
Parameters: - model_class – The type of model to return, by default the model class
of the
lhs
query. - lhs – Left-hand query, either a
SelectQuery
or aCompoundQuery
. - operator – A
Node
instance used to join the two queries, for exampleSQL('UNION')
. - rhs – Right query, either a
SelectQuery
or aCompoundQuery
.
- model_class – The type of model to return, by default the model class
of the
-
prefetch
(sq, *subqueries)¶ Parameters: - sq –
SelectQuery
instance - subqueries – one or more
SelectQuery
instances to prefetch forsq
. You can also pass models, but they will be converted into SelectQueries.
Return type: SelectQuery
with related instances pre-populatedPre-fetch the appropriate instances from the subqueries and apply them to their corresponding parent row in the outer query. This function will eagerly load the related instances specified in the subqueries. This is a technique used to save doing O(n) queries for n rows, and rather is O(k) queries for k subqueries.
For example, consider you have a list of users and want to display all their tweets:
# let's impost some small restrictions on our queries users = User.select().where(User.active == True) tweets = Tweet.select().where(Tweet.published == True) # this will perform 2 queries users_pf = prefetch(users, tweets) # now we can: for user in users_pf: print user.username for tweet in user.tweets_prefetch: print '- ', tweet.content
You can prefetch an arbitrary number of items. For instance, suppose we have a photo site, User -> Photo -> (Comments, Tags). That is, users can post photos, and these photos can have tags and comments on them. If we wanted to fetch a list of users, all their photos, and all the comments and tags on the photos:
users = User.select() published_photos = Photo.select().where(Photo.published == True) published_comments = Comment.select().where( (Comment.is_spam == False) & (Comment.num_flags < 3)) # note that we are just passing the Tag model -- it will be converted # to a query automatically users_pf = prefetch(users, published_photos, published_comments, Tag) # now we can iterate users, photos, and comments/tags for user in users_pf: for photo in user.photo_set_prefetch: for comment in photo.comment_set_prefetch: # ... for tag in photo.tag_set_prefetch: # ...
Note
Subqueries must be related by foreign key and can be arbitrarily deep
Note
For more information, see the Avoiding N+1 queries document.
Warning
prefetch()
can use up lots of RAM when the result set is large, and will not warn you if you are doing something dangerous, so it is up to you to know when to use it. Additionally, because of the semantics of subquerying, there may be some cases when prefetch does not act as you expect (for instnace, when applying aLIMIT
to subqueries, but there may be others) – please report anything you think is a bug to github.- sq –
Database and its subclasses¶
-
class
Database
(database[, threadlocals=False[, autocommit=True[, fields=None[, ops=None[, autorollback=False[, **connect_kwargs]]]]]])¶ Parameters: - database – the name of the database (or filename if using sqlite)
- threadlocals (bool) – whether to store connections in a threadlocal
- autocommit (bool) – automatically commit every query executed by calling
execute()
- fields (dict) – a mapping of
db_field
to database column type, e.g. ‘string’ => ‘varchar’ - ops (dict) – a mapping of operations understood by the querycompiler to expressions
- autorollback (bool) – automatically rollback when an exception occurs while executing a query.
- connect_kwargs – any arbitrary parameters to pass to the database driver when connecting
Note
if your database name is not known when the class is declared, you can pass
None
in as the database name which will mark the database as “deferred” and any attempt to connect while in this state will raise an exception. To initialize your database, call theDatabase.init()
method with the database nameA high-level api for working with the supported database engines.
Database
provides a wrapper around some of the functions performed by theAdapter
, in addition providing support for:- execution of SQL queries
- creating and dropping tables and indexes
-
commit_select = False
Whether to issue a commit after executing a select query. With some engines can prevent implicit transactions from piling up.
-
compiler_class = QueryCompiler
A class suitable for compiling queries
-
field_overrides = {}
A mapping of field types to database column types, e.g.
{'primary_key': 'SERIAL'}
-
foreign_keys = True
Whether the given backend enforces foreign key constraints.
-
for_update = False
Whether the given backend supports selecting rows for update
-
interpolation = '?'
The string used by the driver to interpolate query parameters
-
op_overrides = {}
A mapping of operation codes to string operations, e.g.
{OP_LIKE: 'LIKE BINARY'}
-
quote_char = '"'
The string used by the driver to quote names
-
reserved_tables = []
Table names that are reserved by the backend – if encountered in the application a warning will be issued.
-
sequences = False
Whether the given backend supports sequences
-
subquery_delete_same_table = True
Whether the given backend supports deleting rows using a subquery that selects from the same table
-
init
(database[, **connect_kwargs])¶ If the database was instantiated with
database=None
, the database is said to be in a ‘deferred’ state (see notes) – if this is the case, you can initialize it at any time by calling theinit
method.Parameters: - database – the name of the database (or filename if using sqlite)
- connect_kwargs – any arbitrary parameters to pass to the database driver when connecting
-
connect
()¶ Establishes a connection to the database
Note
If you initialized with
threadlocals=True
, then this will store the connection inside a threadlocal, ensuring that connections are not shared across threads.
-
close
()¶ Closes the connection to the database (if one is open)
Note
If you initialized with
threadlocals=True
, only a connection local to the calling thread will be closed.
-
get_conn
()¶ Return type: a connection to the database, creates one if does not exist
-
get_cursor
()¶ Return type: a cursor for executing queries
-
last_insert_id
(cursor, model)¶ Parameters: - cursor – the database cursor used to perform the insert query
- model – the model class that was just created
Return type: the primary key of the most recently inserted instance
-
rows_affected
(cursor)¶ Return type: number of rows affected by the last query
-
compiler
()¶ Return type: an instance of QueryCompiler
using the field and op overrides specified.
-
execute_sql
(sql[, params=None[, require_commit=True]])¶ Parameters: - sql – a string sql query
- params – a list or tuple of parameters to interpolate
Note
You can configure whether queries will automatically commit by using the
set_autocommit()
andDatabase.get_autocommit()
methods.
-
begin
()¶ Initiate a new transaction. By default not implemented as this is not part of the DB-API 2.0, but provided for API compatibility.
-
commit
()¶ Call
commit()
on the active connection, committing the current transaction
-
rollback
()¶ Call
rollback()
on the active connection, rolling back the current transaction
-
set_autocommit
(autocommit)¶ Parameters: autocommit – a boolean value indicating whether to turn on/off autocommit for the current connection
-
get_autocommit
()¶ Return type: a boolean value indicating whether autocommit is on for the current connection
-
get_tables
()¶ Return type: a list of table names in the database Warning
Not implemented – implementations exist in subclasses
-
get_indexes_for_table
(table)¶ Parameters: table – the name of table to introspect Return type: a list of (index_name, is_unique)
tuplesWarning
Not implemented – implementations exist in subclasses
-
sequence_exists
(sequence_name)¶ Rtype boolean: Warning
Not implemented – implementations exist in subclasses
-
create_index
(model_class, fields[, unique=False])¶ Parameters: - model_class –
Model
table on which to create index - fields – field(s) to create index on (either field instances or field names)
- unique – whether the index should enforce uniqueness
- model_class –
-
create_foreign_key
(model_class, field[, constraint=None])¶ Parameters: - model_class –
Model
table on which to create foreign key constraint - field –
Field
object - constraint (str) – Name to give foreign key constraint.
Manually create a foreign key constraint using an
ALTER TABLE
query. This is primarily used when creating a circular foreign key dependency, for example:PostProxy = Proxy() class User(Model): username = CharField() favorite_post = ForeignKeyField(PostProxy, null=True) class Post(Model): title = CharField() author = ForeignKeyField(User, related_name='posts') PostProxy.initialize(Post) # Create tables. The foreign key from Post -> User will be created # automatically, but the foreign key from User -> Post must be added # manually. User.create_table() Post.create_table() # Manually add the foreign key constraint on `User`, since we could # not add it until we had created the `Post` table. db.create_foreign_key(User, User.favorite_post)
- model_class –
-
create_sequence
(sequence_name)¶ Parameters: sequence_name – name of sequence to create Note
only works with database engines that support sequences
-
drop_table
(model_class[, fail_silently=False[, cascade=False]])¶ Parameters: - model_class –
Model
table to drop - fail_silently (bool) – if
True
, query will add aIF EXISTS
clause - cascade (bool) – drop table with
CASCADE
option.
- model_class –
-
drop_sequence
(sequence_name)¶ Parameters: sequence_name – name of sequence to drop Note
only works with database engines that support sequences
-
create_tables
(models[, safe=False])¶ Parameters: - models (list) – A list of models.
- safe (bool) – Check first whether the table exists before attempting to create it.
This method should be used for creating tables as it will resolve the model dependency graph and ensure the tables are created in the correct order.
Usage:
db.create_tables([User, Tweet, Something], safe=True)
-
drop_tables
(models[, safe=False[, cascade=False]])¶ Parameters: - models (list) – A list of models.
- safe (bool) – Check the table exists before attempting to drop it.
- cascade (bool) – drop table with
CASCADE
option.
This method should be used for dropping tables, as it will resolve the model dependency graph and ensure the tables are dropped in the correct order.
Usage:
db.drop_tables([User, Tweet, Something], safe=True)
-
transaction
()¶ Return a context manager that executes statements in a transaction. If an error is raised inside the context manager, the transaction will be rolled back, otherwise statements are committed when exiting.
# delete a blog instance and all its associated entries, but # do so within a transaction with database.transaction(): blog.delete_instance(recursive=True)
-
commit_on_success
(func)¶ Decorator that wraps the given function in a single transaction, which, upon success will be committed. If an error is raised inside the function, the transaction will be rolled back and the error will be re-raised.
Nested functions can be wrapped with
commit_on_success
- the database will keep a stack and only commit when it reaches the end of the outermost function.Parameters: func – function to decorate @database.commit_on_success def transfer_money(from_acct, to_acct, amt): from_acct.charge(amt) to_acct.pay(amt) return amt
-
savepoint
([sid=None])¶ Return a context manager that executes statements in a savepoint. If an error is raised inside the context manager, the savepoint will be rolled back, otherwise statements are committed when exiting.
Savepoints can be thought of as nested transactions.
Parameters: sid (str) – A string identifier for the savepoint.
-
classmethod
register_fields
(fields)¶ Register a mapping of field overrides for the database class. Used to register custom fields or override the defaults.
Parameters: fields (dict) – A mapping of db_field
to column type
-
classmethod
register_ops
(ops)¶ Register a mapping of operations understood by the QueryCompiler to their SQL equivalent, e.g.
{OP_EQ: '='}
. Used to extend the types of field comparisons.Parameters: fields (dict) – A mapping of db_field
to column type
-
extract_date
(date_part, date_field)¶ Return an expression suitable for extracting a date part from a date field. For instance, extract the year from a
DateTimeField
.Parameters: - date_part (str) – The date part attribute to retrieve. Valid options are: “year”, “month”, “day”, “hour”, “minute” and “second”.
- date_field (Field) – field instance storing a datetime, date or time.
Return type: an expression object.
-
sql_error_handler
(exception, sql, params, require_commit)¶ This hook is called when an error is raised executing a query, allowing your application to inject custom error handling behavior. The default implementation simply reraises the exception.
class SqliteDatabaseCustom(SqliteDatabase): def sql_error_handler(self, exception, sql, params, require_commit): # Perform some custom behavior, for example close the # connection to the database. self.close() # Re-raise the exception. raise exception
-
class
SqliteDatabase
(Database)¶ Database
subclass that works with the “sqlite3” driver. In addition to the default database parameters,SqliteDatabase
also accepts a journal_mode parameter which will configure the journaling mode.To use write-ahead log and connection-per-thread:
db = SqliteDatabase('my_app.db', threadlocals=True, journal_mode='WAL')
Misc¶
-
class
fn
¶ A helper class that will convert arbitrary function calls to SQL function calls.
To express functions in peewee, use the
fn
object. The way it works is anything to the right of the “dot” operator will be treated as a function. You can pass that function arbitrary parameters which can be other valid expressions.For example:
Peewee expression Equivalent SQL fn.Count(Tweet.id).alias('count')
Count(t1."id") AS count
fn.Lower(fn.Substr(User.username, 1, 1))
Lower(Substr(t1."username", 1, 1))
fn.Rand().alias('random')
Rand() AS random
fn.Stddev(Employee.salary).alias('sdv')
Stddev(t1."salary") AS sdv
-
over
([partition_by=None[, order_by=None[, window=None]]])¶ Basic support for SQL window functions.
Parameters: Examples:
# Get the list of employees and the average salary for their dept. query = (Employee .select( Employee.name, Employee.department, Employee.salary, fn.Avg(Employee.salary).over( partition_by=[Employee.department])) .order_by(Employee.name)) # Rank employees by salary. query = (Employee .select( Employee.name, Employee.salary, fn.rank().over( order_by=[Employee.salary]))) # Get a list of page-views, along with avg pageviews for that day. query = (PageView .select( PageView.url, PageView.timestamp, fn.Count(PageView.id).over( partition_by=[fn.date_trunc( 'day', PageView.timestamp)])) .order_by(PageView.timestamp)) # Same as above but using a window class. window = Window(partition_by=[fn.date_trunc('day', PageView.timestamp)]) query = (PageView .select( PageView.url, PageView.timestamp, fn.Count(PageView.id).over(window=window)) .window(window) # Need to include our Window here. .order_by(PageView.timestamp))
-
-
class
SQL
(sql, *params)¶ Add fragments of SQL to a peewee query. For example you might want to reference an aliased name.
Parameters: - sql (str) – Arbitrary SQL string.
- params – Arbitrary query parameters.
# Retrieve user table and "annotate" it with a count of tweets for each # user. query = (User .select(User, fn.Count(Tweet.id).alias('ct')) .join(Tweet, JOIN_LEFT_OUTER) .group_by(User)) # Sort the users by number of tweets. query = query.order_by(SQL('ct DESC'))
-
class
Window
([partition_by=None[, order_by=None]])¶ Create a
WINDOW
definition.Parameters: Examples:
# Get the list of employees and the average salary for their dept. window = Window(partition_by=[Employee.department]).alias('dept_w') query = (Employee .select( Employee.name, Employee.department, Employee.salary, fn.Avg(Employee.salary).over(window)) .window(window) .order_by(Employee.name))
-
class
Proxy
¶ Proxy class useful for situations when you wish to defer the initialization of an object. For instance, you want to define your models but you do not know what database engine you will be using until runtime.
Example:
database_proxy = Proxy() # Create a proxy for our db. class BaseModel(Model): class Meta: database = database_proxy # Use proxy for our DB. class User(BaseModel): username = CharField() # Based on configuration, use a different database. if app.config['DEBUG']: database = SqliteDatabase('local.db') elif app.config['TESTING']: database = SqliteDatabase(':memory:') else: database = PostgresqlDatabase('mega_production_db') # Configure our proxy to use the db we specified in config. database_proxy.initialize(database)
-
class
Node
¶ The
Node
class is the parent class for all composable parts of a query, and forms the basis of peewee’s expression API. The following classes extendNode
:SelectQuery
,UpdateQuery
,InsertQuery
,DeleteQuery
, andRawQuery
.Field
Func
(andfn()
)SQL
Expression
Param
Window
Clause
Entity
Check
Overridden operators:
- Bitwise and- and or- (
&
and|
): combine multiple nodes using the given conjunction. +
,-
,*
,/
and^
(add, subtract, multiply, divide and exclusive-or).==
,!=
,<
,<=
,>
,>=
: create a binary expression using the given comparator.<<
: create an IN expression.>>
: create an IS expression.%
and**
: LIKE and ILIKE.
-
contains
(rhs)¶ Create a binary expression using case-insensitive string search.
-
startswith
(rhs)¶ Create a binary expression using case-insensitive prefix search.
-
endswith
(rhs)¶ Create a binary expression using case-insensitive suffix search.
-
between
(low, high)¶ Create an expression that will match values between
low
andhigh
.
-
regexp
(expression)¶ Match based on regular expression.
-
concat
(rhs)¶ Concatenate the current node with the provided
rhs
.
-
__invert__
()¶ Negate the node. This translates roughly into NOT (<node>).
-
alias
([name=None])¶ Apply an alias to the given node. This translates into <node> AS <name>.
-
asc
()¶ Apply ascending ordering to the given node. This translates into <node> ASC.
-
desc
()¶ Apply descending ordering to the given node. This translates into <node> DESC.