API Reference

Models

class Model(\*kwargs*)

Models provide a 1-to-1 mapping to database tables. Subclasses of Model declare any number of Field instances as class attributes. These fields correspond to columns on the table.

Table-level operations, such as select(), update(), insert(), and delete(), are implemented as classmethods. Row-level operations such as save() and delete_instance() are implemented as instancemethods.

Parameters:kwargs – Initialize the model, assigning the given key/values to the appropriate fields.

Example:

  1. class User(Model):
  2. username = CharField()
  3. join_date = DateTimeField(default=datetime.datetime.now)
  4. is_admin = BooleanField()
  5. 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 given Model.

    Examples of selecting all columns (default):

    1. 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):

    1. (Tweet
    2. .select(Tweet, User)
    3. .join(User)
    4. .order_by(Tweet.created_date.desc()))
  • classmethod update(\*update*)

    Parameters:update – mapping of field-name to expression
    Return type:an UpdateQuery for the given Model

    Example showing users being marked inactive if their registration expired:

    1. q = User.update(active=False).where(User.registration_expired == True)
    2. q.execute() # Execute the query, updating the database.

    Example showing an atomic update:

    1. q = PageView.update(count=PageView.count + 1).where(PageView.url == url)
    2. 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 given Model.

    Example showing creation of a new user:

    1. q = User.insert(username='admin', active=True, registration_expired=False)
    2. q.execute() # perform the insert.

    You can also use Field objects as the keys:

    1. 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:

    1. class User(Model):
    2. username = CharField()
    3. active = BooleanField(default=True)
    4. # This INSERT query will automatically specify `active=True`:
    5. 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 with insert(), 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:

    1. Person.insert_many([
    2. {'first_name': 'Peewee', 'last_name': 'Herman'},
    3. {'first_name': 'Huey'}, # Missing "last_name"!
    4. ])
    Parameters:rows – An iterable containing dictionaries of field-name-to-value.
    Return type:an InsertQuery for the given Model.

    Example of inserting multiple Users:

    1. usernames = ['charlie', 'huey', 'peewee', 'mickey']
    2. row_dicts = ({'username': username} for username in usernames)
    3. # Insert 4 new rows.
    4. User.insert_many(row_dicts).execute()

    Because the rows parameter can be an arbitrary iterable, you can also use a generator:

    1. def get_usernames():
    2. for username in ['charlie', 'huey', 'peewee']:
    3. yield {'username': username}
    4. User.insert_many(get_usernames()).execute()

    Warning

    If you are using SQLite, your SQLite library must be version 3.7.11 or newer to take advantage of bulk inserts.

    Note

    SQLite has a default limit of 999 bound variables per statement. This limit can be modified at compile-time or at run-time, but if modifying at run-time, you can only specify a lower value than the default limit.

    For more information, check out the following SQLite documents:

  • 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 given Model.

    Example of inserting data across tables for denormalization purposes:

    1. source = (User
    2. .select(User.username, fn.COUNT(Tweet.id))
    3. .join(Tweet, JOIN.LEFT_OUTER)
    4. .group_by(User.username))
    5. UserTweetDenorm.insert_from(
    6. [UserTweetDenorm.username, UserTweetDenorm.num_tweets],
    7. source).execute()
  • classmethod delete()

    Return type:a DeleteQuery for the given Model.

    Example showing the deletion of all inactive users:

    1. q = User.delete().where(User.active == False)
    2. 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 given Model

    Example selecting rows from the User table:

    1. q = User.raw('select id, username from users')
    2. for user in q:
    3. 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):

    1. 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 raises DoesNotExist exception

    Get a single row from the database that matches the given query. Raises a <model-class>.DoesNotExist if no rows are returned:

    1. user = User.get(User.username == username, User.active == True)

    This method is also exposed via the SelectQuery, though it takes no parameters:

    1. active = User.select().where(User.active == True)
    2. try:
    3. user = active.where(
    4. (User.username == username) &
    5. (User.active == True)
    6. ).get()
    7. except User.DoesNotExist:
    8. 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 get_or_create([defaults=None[, \*kwargs*]])

    Parameters:
    • defaults (dict) – A dictionary of values to set on newly-created model instances.
    • kwargs – Django-style filters specifying which model to get, and what values to apply to new instances.
    Returns:

    A 2-tuple containing the model instance and a boolean indicating whether the instance was created.

    This function attempts to retrieve a model instance based on the provided filters. If no matching model can be found, a new model is created using the parameters specified by the filters and any values in the defaults dictionary.

    Note

    Use care when calling get_or_create with autocommit=False, as the get_or_create() method will call Database.atomic() to create either a transaction or savepoint.

    Example without get_or_create:

    1. # Without `get_or_create`, we might write:
    2. try:
    3. person = Person.get(
    4. (Person.first_name == 'John') &
    5. (Person.last_name == 'Lennon'))
    6. except Person.DoesNotExist:
    7. person = Person.create(
    8. first_name='John',
    9. last_name='Lennon',
    10. birthday=datetime.date(1940, 10, 9))

    Equivalent code using get_or_create:

    1. person, created = Person.get_or_create(
    2. first_name='John',
    3. last_name='Lennon',
    4. defaults={'birthday': datetime.date(1940, 10, 9)})
  • classmethod alias()

    Return type:ModelAlias instance

    The alias() method is used to create self-joins.

    Example:

    1. Parent = Category.alias()
    2. sq = (Category
    3. .select(Category, Parent)
    4. .join(Parent, on=(Category.parent == Parent.id))
    5. .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, along with any constraints and indexes.

    Example:

    1. database.connect()
    2. 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.

  • 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:

    1. user = User()
    2. user.username = 'some-user' # does not touch the database
    3. 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:

    1. 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).

    If you always want to only save a model’s dirty fields, you can use the Meta option only_save_dirty = True. Then, any time you call Model.save(), by default only the dirty fields will be saved, e.g.

    1. class Person(Model):
    2. first_name = CharField()
    3. last_name = CharField()
    4. dob = DateField()
    5. class Meta:
    6. database = db
    7. only_save_dirty = True
  • 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.

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 or NULL 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 to display
  • 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 and python_value. You can usually get away with just implementing this.

    Parameters:value – arbitrary data from app or backend
    Return type:python data type

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 objects

Additional attributes and values:

max_digits10
decimal_places5
auto_roundFalse
roundingdecimal.DefaultContext.rounding
  • db_field = 'decimal'

class CharField

Stores: small strings (0-255 bytes)

Additional attributes and values:

max_length255
  • db_field = 'string'

class TextField

Stores: arbitrarily large strings

  • db_field = 'text'

class DateTimeField

Stores: python datetime.datetime instances

Accepts a special parameter formats, which contains a list of formats the datetime can be encoded with. The default behavior is:

  1. '%Y-%m-%d %H:%M:%S.%f' # year-month-day hour-minute-second.microsecond
  2. '%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second
  3. '%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:

    1. 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 instances

Accepts a special parameter formats, which contains a list of formats the date can be encoded with. The default behavior is:

  1. '%Y-%m-%d' # year-month-day
  2. '%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second
  3. '%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:

    1. Person.select().where(Person.dob.year == 1983)
  • month

    Same as year, except extract month.

  • day

    Same as year, except extract day.

class TimeField

Stores: python datetime.time instances

Accepts a special parameter formats, which contains a list of formats the time can be encoded with. The default behavior is:

  1. '%H:%M:%S.%f' # hour:minute:second.microsecond
  2. '%H:%M:%S' # hour:minute:second
  3. '%H:%M' # hour:minute
  4. '%Y-%m-%d %H:%M:%S.%f' # year-month-day hour-minute-second.microsecond
  5. '%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:

    1. Event.select().where(Event.time.hour > 17)
  • minute

    Same as hour, except extract minute.

  • second

    Same as hour, except extract second..

class TimestampField

Stores: python datetime.datetime instances (stored as integers)

Accepts a special parameter resolution, which is a power-of-10 up to 10^6. This allows sub-second precision while still using an IntegerField for storage. Default is 1 (second precision).

Also accepts a boolean parameter utc, used to indicate whether the timestamps should be UTC. Default is False.

Finally, the field default is the current timestamp. If you do not want this behavior, then explicitly pass in default=None.

class BooleanField

Stores: True / False

  • db_field = 'bool'

class BlobField

Store arbitrary binary data.

class UUIDField

Store UUID values.

Note

Currently this field is only supported by PostgresqlDatabase.

class BareField

Intended to be used only with SQLite. Since data-types are not enforced, you can declare fields without any data-type. It is also common for SQLite virtual tables to use meta-columns or untyped columns, so for those cases as well you may wish to use an untyped field.

Accepts a special coerce parameter, a function that takes a value coming from the database and converts it into the appropriate Python type.

Note

Currently this field is only supported by SqliteDatabase.

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 for rel_model.
  1. class User(Model):
  2. name = CharField()
  3. class Tweet(Model):
  4. user = ForeignKeyField(User, related_name='tweets')
  5. content = TextField()
  6. # "user" attribute
  7. >>> some_tweet.user
  8. <User: charlie>
  9. # "tweets" related name attribute
  10. >>> for tweet in charlie.tweets:
  11. ... print tweet.content
  12. Some tweet
  13. Another tweet
  14. 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.

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:

  1. class BlogTagThrough(Model):
  2. blog = ForeignKeyField(Blog, related_name='tags')
  3. tag = ForeignKeyField(Tag, related_name='blogs')
  4. class Meta:
  5. primary_key = CompositeKey('blog', 'tag')

Query Types

class Query

The parent class from which all other query classes are derived. 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 instance

    Example selection users where the username is equal to ‘somebody’:

    1. sq = SelectQuery(User).where(User.username == 'somebody')

    Example selecting tweets made by users who are either editors or administrators:

    1. sq = SelectQuery(Tweet).join(User).where(
    2. (User.is_editor == True) |
    3. (User.is_admin == True))

    Example of deleting tweets by users who are no longer active:

    1. dq = DeleteQuery(Tweet).where(
    2. Tweet.user << User.select().where(User.active == False))
    3. 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 current query context and the model passed in.
    • join_type – allows the type of JOIN used to be specified explicitly, one of JOIN.INNER, JOIN.LEFT_OUTER, JOIN.FULL, JOIN.RIGHT_OUTER, or JOIN.CROSS.
    • on – if multiple foreign keys exist between two models, this parameter is the ForeignKeyField to join on.
    Return type:

    a Query instance

    Generate a JOIN clause from the current query context to the model passed in, and establishes model as the new query context.

    Example selecting tweets and joining on user in order to restrict to only those tweets made by “admin” users:

    1. 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:

    1. sq = SelectQuery(User).join(Relationship, on=Relationship.to_user)
  • 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:

    1. 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[, convert=False]])

    Parameters:
    • as_tuple (bool) – return the row as a tuple or a single value
    • convert (bool) – attempt to coerce the selected value to the appropriate data-type based on it’s associated Field type (assuming one exists).
    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.

    1. >>> PageView.select(fn.Count(fn.Distinct(PageView.url))).scalar()
    2. 100 # <-- there are 100 distinct URLs in the pageview table

    This example illustrates the use of the convert argument. When using a SQLite database, datetimes are stored as strings. To select the max datetime, and have it returned as a datetime, we will specify convert=True.

    1. >>> PageView.select(fn.MAX(PageView.timestamp)).scalar()
    2. '2016-04-20 13:37:00.1234'
    3. >>> PageView.select(fn.MAX(PageView.timestamp)).scalar(convert=True)
    4. datetime.datetime(2016, 4, 20, 13, 37, 0, 1234)

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 and username columns are selected. When iterated, will return instances of the User model:

  1. sq = SelectQuery(User, User.id, User.username)
  2. for user in sq:
  3. 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:

  1. sq = (SelectQuery(
  2. User, User, fn.Count(Tweet.id).alias('count'))
  3. .join(Tweet)
  4. .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.

    1. query = User.select()
    2. query = query.select(User.username)
  • from_(\args*)

    Parameters:args – one or more expressions, for example Model or SelectQuery instance(s). if left blank, will default to the table of the given model.
    Return type:SelectQuery
    1. # rather than a join, select from both tables and join with where.
    2. 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:

    1. sq = (User
    2. .select(User, fn.Count(Tweet.id).alias('count'))
    3. .join(Tweet)
    4. .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:

    1. sq = (User
    2. .select(User, fn.Count(Tweet.id).alias('count'))
    3. .join(Tweet)
    4. .group_by(User)
    5. .having(fn.Count(Tweet.id) > 100))
  • order_by(\clauses[, extend=False*])

    Parameters:
    • clauses – a list of fields, calls to field.asc|desc or one or more expressions. If called without any arguments, any pre-existing ORDER BY clause will be removed.
    • extend – When called with extend=True, Peewee will append any to the pre-existing ORDER BY rather than overwriting it.
    Return type:

    SelectQuery

    Example of ordering users by username:

    1. User.select().order_by(User.username)

    Example of selecting tweets and ordering them first by user, then newest first:

    1. query = (Tweet
    2. .select()
    3. .join(User)
    4. .order_by(
    5. User.username,
    6. Tweet.created_date.desc()))

    You can also use + and - prefixes to indicate ascending or descending order if you prefer:

    1. query = (Tweet
    2. .select()
    3. .join(User)
    4. .order_by(
    5. +User.username,
    6. -Tweet.created_date))

    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:

    1. tweet_ct = fn.Count(Tweet.id)
    2. sq = (User
    3. .select(User, tweet_ct.alias('count'))
    4. .join(Tweet)
    5. .group_by(User)
    6. .order_by(tweet_ct.desc(), User.username))

    Example of removing a pre-existing ORDER BY clause:

    1. # Query will be ordered by username.
    2. users = User.select().order_by(User.username)
    3. # Query will be returned in whatever order database chooses.
    4. unordered_users = users.order_by()
  • window(\windows*)

    Parameters:windows (Window) – One or more Window instances.

    Add one or more window definitions to this query.

    1. window = Window(partition_by=[fn.date_trunc('day', PageView.timestamp)])
    2. query = (PageView
    3. .select(
    4. PageView.url,
    5. PageView.timestamp,
    6. fn.Count(PageView.id).over(window=window))
    7. .window(window)
    8. .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:

    SelectQuery

    Shorthand for applying a LIMIT and OFFSET to the query.

    Page indices are 1-based, so page 1 is the first page.

    1. 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 is True then the database will raise an OperationalError if it cannot obtain the lock.

  • with_lock([lock_type=’UPDATE’])

    Return type:SelectQuery

    Indicates that this query shoudl lock rows. A more generic version of the for_update() method.

    Example:

    1. # SELECT * FROM some_model FOR KEY SHARE NOWAIT;
    2. SomeModel.select().with_lock('KEY SHARE NOWAIT')

    Note

    You do not need to include the word FOR.

  • 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.

    1. # iterate over large number of rows.
    2. for obj in Stats.select().iterator():
    3. # do something.
    4. 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 uses IN 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:

    1. # user.id, user.username, tweet.id, tweet.user_id, tweet.message
    2. [1, 'charlie', 1, 1, 'hello'],
    3. [1, 'charlie', 2, 1, 'goodbye'],
    4. [2, 'no-tweets', NULL, NULL, NULL],
    5. [3, 'huey', 3, 3, 'meow'],
    6. [3, 'huey', 4, 3, 'purr'],
    7. [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.

    1. query = (User
    2. .select(User, Tweet)
    3. .join(Tweet, JOIN.LEFT_OUTER)
    4. .order_by(User.username, Tweet.id)
    5. .aggregate_rows()) # .aggregate_rows() tells peewee to de-dupe the rows.
    6. for user in query:
    7. print user.username
    8. for tweet in user.tweets:
    9. print ' ', tweet.message
    10. # Producing the following output:
    11. charlie
    12. hello
    13. goodbye
    14. huey
    15. meow
    16. purr
    17. hiss
    18. 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 and the Using aggregate_rows sub-section.

  • annotate(related_model, aggregation=None)

    Parameters:
    • related_model – related Model on which to perform aggregation, must be linked by ForeignKeyField.
    • aggregation – the type of aggregation to use, e.g. fn.Count(Tweet.id).alias(‘count’)
    Return type:

    SelectQuery

    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”:

    1. >>> User.select().annotate(Tweet)

    If aggregation is None, it will default to fn.Count(related_model.id).alias('count') but can be anything:

    1. >>> user_latest = User.select().annotate(Tweet, fn.Max(Tweet.created_date).alias('latest'))

    Note

    If the ForeignKeyField is nullable, then a LEFT OUTER join may need to be used:

    1. query = (User
    2. .select()
    3. .join(Tweet, JOIN.LEFT_OUTER)
    4. .switch(User) # Switch query context back to `User`.
    5. .annotate(Tweet))
  • 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.

    1. >>> sq = SelectQuery(Tweet)
    2. >>> sq.count()
    3. 45 # number of tweets
    4. >>> deleted_tweets = sq.where(Tweet.status == DELETED)
    5. >>> deleted_tweets.count()
    6. 3 # number of tweets that are marked as deleted
  • wrapped_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

    Wrap the count query in a subquery. Additional overhead but will give correct counts when performing DISTINCT queries or those with GROUP BY clauses.

    Note

    count() will automatically default to wrapped_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().
    1. sq = User.select().where(User.active == True)
    2. if sq.where(User.username == username, User.active == True).exists():
    3. authenticated = True
  • get()

    Return type:Model instance or raises DoesNotExist exception

    Get a single row from the database that matches the given query. Raises a <model-class>.DoesNotExist if no rows are returned:

    1. active = User.select().where(User.active == True)
    2. try:
    3. user = active.where(User.username == username).get()
    4. except User.DoesNotExist:
    5. 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([n=1])

    Parameters:n (int) – Return the first n query results after applying a limit of n records.
    Return type:Model instance, list or None if no results

    Fetch the first n rows from a query. Behind-the-scenes, a LIMIT n is applied. The results of the query are then cached on the query result wrapper so subsequent calls to first() will not cause multiple queries.

    If only one row is requested (default behavior), then the return-type will be either a model instance or None.

    If multiple rows are requested, the return type will either be a list of one to n model instances, or None if no results are found.

  • peek([n=1])

    Parameters:n (int) – Return the first n query results.
    Return type:Model instance, list or None if no results

    Fetch the first n rows from a query. No LIMIT is applied to the query, so the peek() has slightly different semantics from first(), which ensures no more than n rows are requested. The peek method, on the other hand, retains the ability to fetch the entire result set withouth issuing additional queries.

  • 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:

    1. for user in User.select().where(User.active == True):
    2. print user.username
  • __len__()

    Return the number of items in the result set of this query. If all you need is the count of items and do not intend to do anything with the results, call count().

    Warning

    The SELECT query will be executed and the result set will be loaded. If you want to obtain the number of results without also loading the query, use count().

  • __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:

    1. query = User.select().order_by(User.username)
    2. first_user = query[0]
    3. first_five = query[:5]
  • __or__(rhs)

    Parameters:rhs – Either a SelectQuery or a CompoundSelect
    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.

    1. customers = Customer.select(Customer.city).where(Customer.state == 'KS')
    2. stores = Store.select(Store.city).where(Store.state == 'KS')
    3. # Get all cities in kansas where we have either a customer or a store.
    4. all_cities = (customers | stores).order_by(SQL('city'))

    Note

    SQLite does not allow ORDER BY or LIMIT clauses on the components of a compound query, however SQLite does allow these clauses on the final, compound result. This applies to UNION (ALL), INTERSECT, and EXCEPT.

  • __and__(rhs)

    Parameters:rhs – Either a SelectQuery or a CompoundSelect
    Return type:CompoundSelect

    Create an INTERSECT query. The result will contain values that are in both the left and right queries.

    1. customers = Customer.select(Customer.city).where(Customer.state == 'KS')
    2. stores = Store.select(Store.city).where(Store.state == 'KS')
    3. # Get all cities in kanasas where we have both customers and stores.
    4. cities = (customers & stores).order_by(SQL('city'))
  • __sub__(rhs)

    Parameters:rhs – Either a SelectQuery or a CompoundSelect
    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.

    1. customers = Customer.select(Customer.city).where(Customer.state == 'KS')
    2. stores = Store.select(Store.city).where(Store.state == 'KS')
    3. # Get all cities in kanasas where we have customers but no stores.
    4. cities = (customers - stores).order_by(SQL('city'))
  • __xor__(rhs)

    Parameters:rhs – Either a SelectQuery or a CompoundSelect
    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.

    1. customers = Customer.select(Customer.city).where(Customer.state == 'KS')
    2. stores = Store.select(Store.city).where(Store.state == 'KS')
    3. # Get all cities in kanasas where we have either customers with no
    4. # store, or a store with no customers.
    5. cities = (customers ^ stores).order_by(SQL('city'))

class UpdateQuery(model_class, \*kwargs*)

Parameters:
  • modelModel 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:

  1. uq = UpdateQuery(User, active=False).where(User.registration_expired == True)
  2. uq.execute() # Perform the actual update

Example of an atomic update:

  1. atomic_update = UpdateQuery(PageCount, count = PageCount.count + 1).where(
  2. PageCount.url == url)
  3. atomic_update.execute() # will perform the actual update
  • execute()

    Return type:Number of rows updated

    Performs the query

  • returning(\returning*)

    Parameters:returning – A list of model classes, field instances, functions or expressions. If no argument is provided, all columns for the given model will be selected. To clear any existing values, pass in None.
    Return type:a UpdateQuery for the given Model.

    Add a RETURNING clause to the query, which will cause the UPDATE to compute return values based on each row that was actually updated.

    When the query is executed, rather than returning the number of rows updated, an iterator will be returned that yields the updated objects.

    Note

    Currently only PostgresqlDatabase supports this feature.

    Example:

    1. # Disable all users whose registration expired, and return the user
    2. # objects that were updated.
    3. query = (User
    4. .update(active=False)
    5. .where(User.registration_expired == True)
    6. .returning(User))
    7. # We can iterate over the users that were updated.
    8. for updated_user in query.execute():
    9. send_activation_email(updated_user.email)

    For more information, check out the RETURNING clause docs.

  • tuples()

    Return type:UpdateQuery

    Note

    This method should only be used in conjunction with a call to returning().

    When the updated results are returned, they will be returned as row tuples.

  • dicts()

    Return type:UpdateQuery

    Note

    This method should only be used in conjunction with a call to returning().

    When the updated results are returned, they will be returned as dictionaries mapping column to value.

  • on_conflict([action=None])

    Add a SQL ON CONFLICT clause with the specified action to the given UPDATE query. Valid actions are:

    • ROLLBACK
    • ABORT
    • FAIL
    • IGNORE
    • REPLACE

    Specifying None for the action will execute a normal UPDATE query.

    Note

    This feature is only available on SQLite databases.

class InsertQuery(model_class[, field_dict=None[, rows=None[, fields=None[, query=None[, validate_fields=False]]]]])

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.
  • validate_fields (bool) – Check that every column referenced in the insert query has a corresponding field on the model. If validation is enabled and then fails, a KeyError is raised.

Basic example:

  1. >>> fields = {'username': 'admin', 'password': 'test', 'active': True}
  2. >>> iq = InsertQuery(User, fields)
  3. >>> iq.execute() # insert new row and return primary key
  4. 2L

Example inserting multiple rows:

  1. users = [
  2. {'username': 'charlie', 'active': True},
  3. {'username': 'peewee', 'active': False},
  4. {'username': 'huey', 'active': True}]
  5. iq = InsertQuery(User, rows=users)
  6. iq.execute()

Example inserting using a query as the data source:

  1. query = (User
  2. .select(User.username, fn.COUNT(Tweet.id))
  3. .join(Tweet, JOIN.LEFT_OUTER)
  4. .group_by(User.username))
  5. iq = InsertQuery(
  6. UserTweetDenorm,
  7. fields=[UserTweetDenorm.username, UserTweetDenorm.num_tweets],
  8. query=query)
  9. iq.execute()
  • execute()

    Return type:primary key of the new row

    Performs the query

  • upsert([upsert=True])

    Perform an INSERT OR REPLACE query with SQLite. MySQL databases will issue a REPLACE query. Currently this feature is not supported for Postgres databases, but the 9.5 syntax will be added soon.

    Note

    This feature is only available on SQLite and MySQL databases.

  • on_conflict([action=None])

    Add a SQL ON CONFLICT clause with the specified action to the given INSERT query. Specifying REPLACE is equivalent to using the upsert() method. Valid actions are:

    • ROLLBACK
    • ABORT
    • FAIL
    • IGNORE
    • REPLACE

    Specifying None for the action will execute a normal INSERT query.

    Note

    This feature is only available on SQLite databases.

  • return_id_list([return_id_list=True])

    By default, when doing bulk INSERTs, peewee will not return the list of generated primary keys. However, if the database supports returning primary keys via INSERT ... RETURNING, this method instructs peewee to return the generated list of IDs.

    Note

    Currently only PostgreSQL supports this behavior. While other databases support bulk inserts, they will simply return True instead.

    Example:

    1. usernames = [
    2. {'username': username}
    3. for username in ['charlie', 'huey', 'mickey']]
    4. query = User.insert_many(usernames).return_id_list()
    5. user_ids = query.execute()
    6. print user_ids
    7. # prints something like [1, 2, 3]
  • returning(\returning*)

    Parameters:returning – A list of model classes, field instances, functions or expressions. If no argument is provided, all columns for the given model will be selected. To clear any existing values, pass in None.
    Return type:a InsertQuery for the given Model.

    Add a RETURNING clause to the query, which will cause the INSERT to compute return values based on each row that was inserted.

    When the query is executed, rather than returning the primary key of the new row(s), an iterator will be returned that yields the inserted objects.

    Note

    Currently only PostgresqlDatabase supports this feature.

    Example:

    1. # Create some users, retrieving the list of IDs assigned to them.
    2. query = (User
    3. .insert_many(list_of_user_data)
    4. .returning(User))
    5. # We can iterate over the users that were created.
    6. for new_user in query.execute():
    7. # Do something with the new user's ID...
    8. do_something(new_user.id)

    For more information, check out the RETURNING clause docs.

  • tuples()

    Return type:InsertQuery

    Note

    This method should only be used in conjunction with a call to returning().

    When the inserted results are returned, they will be returned as row tuples.

  • dicts()

    Return type:InsertQuery

    Note

    This method should only be used in conjunction with a call to returning().

    When the inserted results are returned, they will be returned as dictionaries mapping column to value.

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:

  1. dq = DeleteQuery(User).where(User.active == False)
  • execute()

    Return type:Number of rows deleted

    Performs the query

  • returning(\returning*)

    Parameters:returning – A list of model classes, field instances, functions or expressions. If no argument is provided, all columns for the given model will be selected. To clear any existing values, pass in None.
    Return type:a DeleteQuery for the given Model.

    Add a RETURNING clause to the query, which will cause the DELETE to compute return values based on each row that was removed from the database.

    When the query is executed, rather than returning the number of rows deleted, an iterator will be returned that yields the deleted objects.

    Note

    Currently only PostgresqlDatabase supports this feature.

    Example:

    1. # Create some users, retrieving the list of IDs assigned to them.
    2. query = (User
    3. .delete()
    4. .where(User.account_expired == True)
    5. .returning(User))
    6. # We can iterate over the user objects that were deleted.
    7. for deleted_user in query.execute():
    8. # Do something with the deleted user.
    9. notify_account_deleted(deleted_user.email)

    For more information, check out the RETURNING clause docs.

  • tuples()

    Return type:DeleteQuery

    Note

    This method should only be used in conjunction with a call to returning().

    When the deleted results are returned, they will be returned as row tuples.

  • dicts()

    Return type:DeleteQuery

    Note

    This method should only be used in conjunction with a call to returning().

    When the deleted results are returned, they will be returned as dictionaries mapping column to value.

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:

  1. >>> rq = RawQuery(User, 'SELECT * FROM users WHERE username = ?', 'admin')
  2. >>> for obj in rq.execute():
  3. ... print obj
  4. <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 a CompoundQuery.
  • operator – A string used to join the two queries, for example ‘UNION’.
  • rhs – Right query, either a SelectQuery or a CompoundQuery.

prefetch(sq, \subqueries*)

Parameters:
  • sqSelectQuery instance
  • subqueries – one or more SelectQuery instances to prefetch for sq. You can also pass models, but they will be converted into SelectQueries. If you wish to specify a particular model to join against, you can pass a 2-tuple of (query_or_model, join_model).
Return type:

SelectQuery with related instances pre-populated

Pre-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:

  1. # let's impost some small restrictions on our queries
  2. users = User.select().where(User.active == True)
  3. tweets = Tweet.select().where(Tweet.published == True)
  4. # this will perform 2 queries
  5. users_pf = prefetch(users, tweets)
  6. # now we can:
  7. for user in users_pf:
  8. print user.username
  9. for tweet in user.tweets_prefetch:
  10. 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:

  1. users = User.select()
  2. published_photos = Photo.select().where(Photo.published == True)
  3. published_comments = Comment.select().where(
  4. (Comment.is_spam == False) &
  5. (Comment.num_flags < 3))
  6. # note that we are just passing the Tag model -- it will be converted
  7. # to a query automatically
  8. users_pf = prefetch(users, published_photos, published_comments, Tag)
  9. # now we can iterate users, photos, and comments/tags
  10. for user in users_pf:
  11. for photo in user.photo_set_prefetch:
  12. for comment in photo.comment_set_prefetch:
  13. # ...
  14. for tag in photo.tag_set_prefetch:
  15. # ...

Note

Subqueries must be related by foreign key and can be arbitrarily deep

Note

For more information, see the Avoiding N+1 queries document and the Using prefetch sub-section.

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 instance, when applying a LIMIT to subqueries, but there may be others) – please report anything you think is a bug to github.

Database and its subclasses

class Database(database[, threadlocals=True[, autocommit=True[, fields=None[, ops=None[, autorollback=False[, use_speedups=True[, \*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.
  • use_speedups (bool) – use the Cython speedups module to improve performance of some queries.
  • connect_kwargs – any arbitrary parameters to pass to the database driver when connecting

The connect_kwargs dictionary is used for vendor-specific parameters that will be passed back directly to your database driver, allowing you to specify the user, host and password, for instance. For more information and examples, see the vendor-specific parameters document.

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 the Database.init() method with the database name.

For an in-depth discussion of run-time database configuration, see the Run-time database configuration section.

A high-level API for working with the supported database engines. The database class:

  • Manages the underlying database connection.
  • Executes queries.
  • Manage transactions and savepoints.
  • Create and drop tables and indexes.
  • Introspect the database.

  • 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

  • compound_operations = ['UNION', 'INTERSECT', 'EXCEPT']

    Supported compound query operations.

  • compound_select_parentheses = False

    Whether UNION (or other compound SELECT queries) allow parentheses around the queries.

  • distinct_on = False

    Whether the database supports DISTINCT ON statements.

  • drop_cascade = False

    Whether the database supports cascading drop table 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

  • for_update_nowait = False

    Whether the given backend supports selecting rows for update

  • insert_many = True

    Whether the database supports multiple VALUES clauses for INSERT queries.

  • insert_returning = False

    Whether the database supports returning the primary key for newly inserted rows.

  • 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.

  • returning_clause = False

    Whether the database supports RETURNING clauses for UPDATE, INSERT and DELETE queries.

    Note

    Currently only PostgresqlDatabase supports this.

    See the following for more information:

  • savepoints = True

    Whether the given backend supports savepoints.

  • 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

  • window_functions = False

    Whether the given backend supports window functions.

  • init(database[, \*connect_kwargs*])

    This method is used to initialize a deferred database. For details on configuring your database at run-time, see the Run-time database configuration section.

    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

    By default, connections will be stored on a threadlocal, ensuring connections are not shared across threads. To disable this behavior, initialize the database with threadlocals=False.

  • 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.

  • initialize_connection(conn)

    Perform additional intialization on a newly-opened connection. For example, if you are using SQLite you may want to enable foreign key constraint enforcement (off by default).

    Here is how you might use this hook to load a SQLite extension:

    1. class CustomSqliteDatabase(SqliteDatabase):
    2. def initialize_connection(self, conn):
    3. conn.load_extension('fts5')
  • 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(clause)

    Parameters:clause (Node) – a Node instance or subclass (e.g. a SelectQuery).

    The clause will be compiled into SQL then sent to the execute_sql() method.

  • 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() and Database.get_autocommit() methods.

  • begin([lock_type=None])

    Initiate a new transaction. By default not implemented as this is not part of the DB-API 2.0, but provided for API compatibility and to allow SQLite users to specify the isolation level when beginning transactions.

    For SQLite users, the valid isolation levels for lock_type are:

    • exclusive
    • immediate
    • deferred

    Example usage:

    1. # Calling transaction() in turn calls begin('exclusive').
    2. with db.transaction('exclusive'):
    3. # No other readers or writers allowed while this is active.
    4. (Account
    5. .update(Account.balance=Account.balance - 100)
    6. .where(Account.id == from_acct)
    7. .execute())
    8. (Account
    9. .update(Account.balance=Account.balance + 100)
    10. .where(Account.id == to_acct)
    11. .execute())
  • 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.
  • get_autocommit()

    Return type:a boolean value indicating whether autocommit is enabled.
  • get_tables([schema=None])

    Return type:a list of table names in the database.
  • get_indexes(table[, schema=None])

    Return type:a list of IndexMetadata instances, representing the indexes for the given table.
  • get_columns(table[, schema=None])

    Return type:a list of ColumnMetadata instances, representing the columns for the given table.
  • get_primary_keys(table[, schema=None])

    Return type:a list containing the primary key column name(s) for the given table.
  • get_foreign_keys(table[, schema=None])

    Return type:a list of ForeignKeyMetadata instances, representing the foreign keys for the given table.
  • sequence_exists(sequence_name)

    Rtype boolean:
  • create_table(model_class[, safe=True])

    Parameters:
    • model_classModel class.
    • safe (bool) – If True, the table will not be created if it already exists.

    Warning

    Unlike Model.create_table(), this method does not create indexes or constraints. This method will only create the table itself. If you wish to create the table along with any indexes and constraints, use either Model.create_table() or Database.create_tables().

  • create_index(model_class, fields[, unique=False])

    Parameters:
    • model_classModel 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
  • create_foreign_key(model_class, field[, constraint=None])

    Parameters:
    • model_classModel table on which to create foreign key constraint
    • fieldField 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:

    1. DeferredPost = DeferredRelation()
    2. class User(Model):
    3. username = CharField()
    4. favorite_post = ForeignKeyField(DeferredPost, null=True)
    5. class Post(Model):
    6. title = CharField()
    7. author = ForeignKeyField(User, related_name='posts')
    8. DeferredPost.set_model(Post)
    9. # Create tables. The foreign key from Post -> User will be created
    10. # automatically, but the foreign key from User -> Post must be added
    11. # manually.
    12. User.create_table()
    13. Post.create_table()
    14. # Manually add the foreign key constraint on `User`, since we could
    15. # not add it until we had created the `Post` table.
    16. db.create_foreign_key(User, User.favorite_post)
  • 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_classModel table to drop
    • fail_silently (bool) – if True, query will add a IF EXISTS clause
    • cascade (bool) – drop table with CASCADE option.
  • 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. This method will also create any indexes and constraints defined on the models.

    Usage:

    1. 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:

    1. db.drop_tables([User, Tweet, Something], safe=True)
  • atomic([transaction_type=None])

    Execute statements in either a transaction or a savepoint. The outer-most call to atomic will use a transaction, and any subsequent nested calls will use savepoints.

    Parameters:transaction_type (str) – Specify isolation level. This parameter only has effect on SQLite databases, and furthermore, only affects the outer-most call to atomic(). For more information, see transaction().

    atomic can be used as either a context manager or a decorator.

    Note

    For most use-cases, it makes the most sense to always use atomic() when you wish to execute queries in a transaction. The benefit of using atomic is that you do not need to manually keep track of the transaction stack depth, as this will be managed for you.

    Context manager example code:

    1. with db.atomic() as txn:
    2. perform_some_operations()
    3. with db.atomic() as nested_txn:
    4. do_other_things()
    5. if something_bad_happened():
    6. # Roll back these changes, but preserve the changes
    7. # made in the outer block.
    8. nested_txn.rollback()

    Decorator example code:

    1. @db.atomic()
    2. def create_user(username):
    3. # This function will execute in a transaction/savepoint.
    4. return User.create(username=username)
  • transaction([transaction_type=None])

    Execute statements in a transaction using either a context manager or decorator. If an error is raised inside the wrapped block, the transaction will be rolled back, otherwise statements are committed when exiting. Transactions can also be explicitly rolled back or committed within the transaction block by calling rollback() or commit(). If you manually commit or roll back, a new transaction will be started automatically.

    Nested blocks can be wrapped with transaction - the database will keep a stack and only commit when it reaches the end of the outermost function / block.

    Parameters:transaction_type (str) – Specify isolation level, SQLite only.

    Context manager example code:

    ```

    delete a blog instance and all its associated entries, but

    do so within a transaction

    with database.transaction():

    1. blog.delete_instance(recursive=True)
  1. # Explicitly roll back a transaction.
  2. with database.transaction() as txn:
  3. do_some_stuff()
  4. if something_bad_happened():
  5. # Roll back any changes made within this block.
  6. txn.rollback()
  7. ```
  8. Decorator example code:
  9. ```
  10. @database.transaction()
  11. def transfer_money(from_acct, to_acct, amt):
  12. from_acct.charge(amt)
  13. to_acct.pay(amt)
  14. return amt
  15. ```
  16. SQLite users can specify the isolation level by specifying one of the following values for `transaction_type`:
  17. - `exclusive`
  18. - `immediate`
  19. - `deferred`
  20. Example usage:
  21. ```
  22. with db.transaction('exclusive'):
  23. # No other readers or writers allowed while this is active.
  24. (Account
  25. .update(Account.balance=Account.balance - 100)
  26. .where(Account.id == from_acct)
  27. .execute())
  28. (Account
  29. .update(Account.balance=Account.balance + 100)
  30. .where(Account.id == to_acct)
  31. .execute())
  32. ```
  • commit_on_success(func)

    Note

    Use atomic() or transaction() instead.

  • savepoint([sid=None])

    Execute statements in a savepoint using either a context manager or decorator. If an error is raised inside the wrapped block, the savepoint will be rolled back, otherwise statements are committed when exiting. Like transaction(), a savepoint can also be explicitly rolled-back or committed by calling rollback() or commit(). If you manually commit or roll back, a new savepoint will not be created.

    Savepoints can be thought of as nested transactions.

    Parameters:sid (str) – An optional string identifier for the savepoint.

    Context manager example code:

    1. with db.transaction() as txn:
    2. do_some_stuff()
    3. with db.savepoint() as sp1:
    4. do_more_things()
    5. with db.savepoint() as sp2:
    6. even_more()
    7. # Oops, something bad happened, roll back
    8. # just the changes made in this block.
    9. if something_bad_happened():
    10. sp2.rollback()
  • execution_context([with_transaction=True])

    Create an ExecutionContext context manager or decorator. Blocks wrapped with an ExecutionContext will run using their own connection. By default, the wrapped block will also run in a transaction, although this can be disabled specifyin with_transaction=False.

    For more explanation of ExecutionContext, see the Advanced Connection Management section.

    Warning

    ExecutionContext is very new and has not been tested extensively.

  • 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.

  • truncate_date(date_part, date_field)

    Return an expression suitable for truncating a date / datetime to the given resolution. This can be used, for example, to group a collection of timestamps by day.

    Parameters:
    • date_part (str) – The date part to truncate to. 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.

    Example:

    1. # Get tweets from today.
    2. tweets = Tweet.select().where(
    3. db.truncate_date('day', Tweet.timestamp) == datetime.date.today())

class SqliteDatabase(Database)

Database subclass that works with the sqlite3 driver (or pysqlite2). In addition to the default database parameters, SqliteDatabase also accepts a journal_mode parameter which will configure the journaling mode.

Note

If you have both sqlite3 and pysqlite2 installed on your system, peewee will use whichever points at a newer version of SQLite.

Note

SQLite is unique among the databases supported by Peewee in that it allows a high degree of customization by the host application. This means you can do things like write custom functions or aggregates in Python and then call them from your SQL queries. This feature, and many more, are available through the SqliteExtDatabase, part of playhouse.sqlite_ext. I strongly recommend you use SqliteExtDatabase as it exposes many of the features that make SQLite so powerful.

Custom parameters:

Parameters:
  • journal_mode (str) – Journaling mode.
  • pragmas (list) – List of 2-tuples containing PRAGMA statements to run against new connections.

SQLite allows run-time configuration of a number of parameters through PRAGMA statements (documentation). These statements are typically run against a new database connection. To run one or more PRAGMA statements against new connections, you can specify them as a list of 2-tuples containing the pragma name and value:

  1. db = SqliteDatabase('my_app.db', pragmas=(
  2. ('journal_mode', 'WAL'),
  3. ('cache_size', 10000),
  4. ('mmap_size', 1024 * 1024 * 32),
  5. ))
  • insert_many = True *if* using SQLite 3.7.11.0 or newer.

class MySQLDatabase(Database)

Database subclass that works with either “MySQLdb” or “pymysql”.

  • commit_select = True

  • compound_operations = ['UNION']

  • for_update = True

  • subquery_delete_same_table = False

class PostgresqlDatabase(Database)

Database subclass that works with the “psycopg2” driver

  • commit_select = True

  • compound_select_parentheses = True

  • distinct_on = True

  • for_update = True

  • for_update_nowait = True

  • insert_returning = True

  • returning_clause = True

  • sequences = True

  • window_functions = True

  • register_unicode = True

    Control whether the UNICODE and UNICODEARRAY psycopg2 extensions are loaded automatically.

Transaction, Savepoint and ExecutionContext

The easiest way to create transactions and savepoints is to use Database.atomic(). The atomic() method will create a transaction or savepoint depending on the level of nesting.

  1. with db.atomic() as txn:
  2. # The outer-most call will be a transaction.
  3. with db.atomic() as sp:
  4. # Nested calls will be savepoints instead.
  5. execute_some_statements()

class transaction(database)

Context manager that encapsulates a database transaction. Statements executed within the wrapped block will be committed at the end of the block unless an exception occurs, in which case any changes will be rolled back.

Warning

Transactions should not be nested as this could lead to unpredictable behavior in the event of an exception in a nested block. If you wish to use nested transactions, use the atomic() method, which will create a transaction at the outer-most layer and use savepoints for nested blocks.

Note

In practice you should not create transaction objects directly, but rather use the Database.transaction() method.

  • commit()

    Manually commit any pending changes and begin a new transaction.

  • rollback()

    Manually roll-back any pending changes and begin a new transaction.

class savepoint(database[, sid=None])

Context manager that encapsulates a savepoint (nested transaction). Statements executed within the wrapped block will be committed at the end of the block unless an exception occurs, in which case any changes will be rolled back.

Warning

Savepoints must be created within a transaction. It is recommended that you use atomic() instead of manually managing the transaction+savepoint stack.

Note

In practice you should not create savepoint objects directly, but rather use the Database.savepoint() method.

  • commit()

    Manually commit any pending changes. If the savepoint is manually committed and additional changes are made, they will be executed in the context of the outer block.

  • rollback()

    Manually roll-back any pending changes. If the savepoint is manually rolled-back and additional changes are made, they will be executed in the context of the outer block.

class ExecutionContext(database[, with_transaction=True])

ExecutionContext provides a way to explicitly run statements in a dedicated connection. Typically a single database connection is maintained per-thread, but in some situations you may wish to explicitly force a new, separate connection. To accomplish this, you can create an ExecutionContext. Statements executed in the wrapped block will be run in a transaction by default, though you can disable this by specifying with_transaction=False.

Note

Rather than instantiating ExecutionContext directly, use Database.execution_context().

Example code:

  1. # This will return the connection associated with the current thread.
  2. conn = db.get_conn()
  3. with db.execution_context():
  4. # This will be a new connection object. If you are using the
  5. # connection pool, it may be an unused connection from the pool.
  6. ctx_conn = db.get_conn()
  7. # This statement is executed using the new `ctx_conn`.
  8. User.create(username='huey')
  9. # At the end of the wrapped block, the connection will be closed and the
  10. # transaction, if one exists, will be committed.
  11. # This statement is executed using the regular `conn`.
  12. User.create(username='mickey')

class Using(database, models[, with_transaction=True])

For the duration of the wrapped block, all queries against the given models will use the specified database. Optionally these queries can be run outside a transaction by specifying with_transaction=False.

Using provides, in short, a way to run queries on a list of models using a manually specified database.

Parameters:
  • database – a Database instance.
  • models – a list of Model classes to use with the given database.
  • with_transaction – Whether the wrapped block should be run in a transaction.

Warning

The Using context manager does not do anything to manage the database connections, so it the user’s responsibility to make sure that you close the database explicitly.

Example:

  1. master = PostgresqlDatabase('master')
  2. replica = PostgresqlDatabase('replica')
  3. class Data(Model):
  4. value = IntegerField()
  5. class Meta:
  6. database = master
  7. # All these queries use the "master" database,
  8. # since that is what our Data model was configured
  9. # to use.
  10. for i in range(10):
  11. Data.create(value=i)
  12. Data.insert_many({Data.value: j} for j in range(100, 200)).execute()
  13. # To use the read replica, we can use the Using context manager.
  14. with Using(read_replica, [Data]):
  15. # Query is executed against the read replica.
  16. n_data = Data.select().count()
  17. # Since we did not specify this model in the list passed
  18. # to Using, it will use whatever database it was defined with.
  19. other_count = SomeOtherModel.select().count()

Metadata Types

class IndexMetadata(name, sql, columns, unique, table)

  • name

    The name of the index.

  • sql

    The SQL query used to generate the index.

  • columns

    A list of columns that are covered by the index.

  • unique

    A boolean value indicating whether the index has a unique constraint.

  • table

    The name of the table containing this index.

class ColumnMetadata(name, data_type, null, primary_key, table)

  • name

    The name of the column.

  • data_type

    The data type of the column

  • null

    A boolean value indicating whether NULL is permitted in this column.

  • primary_key

    A boolean value indicating whether this column is a primary key.

  • table

    The name of the table containing this column.

class ForeignKeyMetadata(column, dest_table, dest_column, table)

  • column

    The column containing the foreign key (the “source”).

  • dest_table

    The table referenced by the foreign key.

  • dest_column

    The column referenced by the foreign key (on dest_table).

  • table

    The name of the table containing this foreign key.

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 expressionEquivalent 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[, start=None[, end=None[, window=None]]]]])

    Basic support for SQL window functions.

    Parameters:
    • partition_by (list) – List of Node instances to partition by.
    • order_by (list) – List of Node instances to use for ordering.
    • start – The start of the frame of the window query.
    • end – The end of the frame of the window query.
    • window (Window) – A Window instance to use for this aggregate.

    Examples:

    1. # Get the list of employees and the average salary for their dept.
    2. query = (Employee
    3. .select(
    4. Employee.name,
    5. Employee.department,
    6. Employee.salary,
    7. fn.Avg(Employee.salary).over(
    8. partition_by=[Employee.department]))
    9. .order_by(Employee.name))
    10. # Rank employees by salary.
    11. query = (Employee
    12. .select(
    13. Employee.name,
    14. Employee.salary,
    15. fn.rank().over(
    16. order_by=[Employee.salary])))
    17. # Get a list of page-views, along with avg pageviews for that day.
    18. query = (PageView
    19. .select(
    20. PageView.url,
    21. PageView.timestamp,
    22. fn.Count(PageView.id).over(
    23. partition_by=[fn.date_trunc(
    24. 'day', PageView.timestamp)]))
    25. .order_by(PageView.timestamp))
    26. # Same as above but using a window class.
    27. window = Window(partition_by=[fn.date_trunc('day', PageView.timestamp)])
    28. query = (PageView
    29. .select(
    30. PageView.url,
    31. PageView.timestamp,
    32. fn.Count(PageView.id).over(window=window))
    33. .window(window) # Need to include our Window here.
    34. .order_by(PageView.timestamp))
    35. # Get the list of times along with the last time.
    36. query = (Times
    37. .select(
    38. Times.time,
    39. fn.LAST_VALUE(Times.time).over(
    40. order_by=[Times.time],
    41. start=Window.preceding(),
    42. end=Window.following())))

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.
  1. # Retrieve user table and "annotate" it with a count of tweets for each
  2. # user.
  3. query = (User
  4. .select(User, fn.Count(Tweet.id).alias('ct'))
  5. .join(Tweet, JOIN.LEFT_OUTER)
  6. .group_by(User))
  7. # Sort the users by number of tweets.
  8. query = query.order_by(SQL('ct DESC'))

class Window([partition_by=None[, order_by=None[, start=None[, end=None]]]])

Create a WINDOW definition.

Parameters:
  • partition_by (list) – List of Node instances to partition by.
  • order_by (list) – List of Node instances to use for ordering.
  • start – The start of the frame of the window query.
  • end – The end of the frame of the window query.

Examples:

  1. # Get the list of employees and the average salary for their dept.
  2. window = Window(partition_by=[Employee.department]).alias('dept_w')
  3. query = (Employee
  4. .select(
  5. Employee.name,
  6. Employee.department,
  7. Employee.salary,
  8. fn.Avg(Employee.salary).over(window))
  9. .window(window)
  10. .order_by(Employee.name))
  • static preceding([value=None])

    Return an expression appropriate for passing in to the start or end clause of a Window object. If value is not provided, then it will be UNBOUNDED PRECEDING.

  • static following([value=None])

    Return an expression appropriate for passing in to the start or end clause of a Window object. If value is not provided, then it will be UNBOUNDED FOLLOWING.

class DeferredRelation

Used to reference a not-yet-created model class. Stands in as a placeholder for the related model of a foreign key. Useful for circular references.

  1. DeferredPost = DeferredRelation()
  2. class User(Model):
  3. username = CharField()
  4. # `Post` is not available yet, it is declared below.
  5. favorite_post = ForeignKeyField(DeferredPost, null=True)
  6. class Post(Model):
  7. # `Post` comes after `User` since it refers to `User`.
  8. user = ForeignKeyField(User)
  9. title = CharField()
  10. DeferredPost.set_model(Post) # Post is now available.
  • set_model(model)

    Replace the placeholder with the correct model class.

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:

  1. database_proxy = Proxy() # Create a proxy for our db.
  2. class BaseModel(Model):
  3. class Meta:
  4. database = database_proxy # Use proxy for our DB.
  5. class User(BaseModel):
  6. username = CharField()
  7. # Based on configuration, use a different database.
  8. if app.config['DEBUG']:
  9. database = SqliteDatabase('local.db')
  10. elif app.config['TESTING']:
  11. database = SqliteDatabase(':memory:')
  12. else:
  13. database = PostgresqlDatabase('mega_production_db')
  14. # Configure our proxy to use the db we specified in config.
  15. database_proxy.initialize(database)
  • initialize(obj)

    Parameters:obj – The object to proxy to.

    Once initialized, the attributes and methods on obj can be accessed directly via the Proxy instance.

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 extend Node:

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 and high.

  • regexp(expression)

    Match based on regular expression.

  • concat(rhs)

    Concatenate the current node with the provided rhs.

    Warning

    In order for this method to work with MySQL, the MySQL session must be set to use PIPES_AS_CONCAT.

    To reliably concatenate strings with MySQL, use fn.CONCAT(s1, s2...) instead.

  • is_null([is_null=True])

    Create an expression testing whether the Node is (or is not) NULL.

    1. # Find all categories whose parent column is NULL.
    2. root_nodes = Category.select().where(Category.parent.is_null())
    3. # Find all categores whose parent is NOT NULL.
    4. child_nodes = Category.select().where(Category.parent.is_null(False))

    To simplify things, peewee will generate the correct SQL for equality and inequality. The is_null() method is provided simply for readability.

    1. # Equivalent to the previous queries -- peewee will translate these
    2. # into `IS NULL` and `IS NOT NULL`:
    3. root_nodes = Category.select().where(Category.parent == None)
    4. child_nodes = Category.select().where(Category.parent != None)
  • __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.

  • bind_to(model_class)

    Bind the results of an expression to a specific model type. Useful when adding expressions to a select, where the result of the expression should be placed on a particular joined instance.

  • classmethod extend([name=None[, clone=False]])

    Decorator for adding the decorated function as a new method on Node and its subclasses. Useful for adding implementation-specific features to all node types.

    Parameters:
    • name (str) – Method name. If not provided the name of the wrapped function will be used.
    • clone (bool) – Whether this method should return a clone. This is generally true when the method mutates the internal state of the node.

    Example:

    1. # Add a `cast()` method to all nodes using the '::' operator.
    2. PostgresqlDatabase.register_ops({'::', '::'})
    3. @Node.extend()
    4. def cast(self, as_type):
    5. return Expression(self, '::', SQL(as_type))
    6. # Let's pretend we want to find all data points whose numbers
    7. # are palindromes. Note that we can use the new *cast* method
    8. # on both fields and with the `fn` helper:
    9. reverse_val = fn.REVERSE(DataModel.value.cast('str')).cast('int')
    10. query = (DataPoint
    11. .select()
    12. .where(DataPoint.value == reverse_val))

    Note

    To remove an extended method, simply call delattr on the class the method was originally added to.