MyTetra Share
Делитесь знаниями!
Query operators / операторы peewee
Время создания: 12.05.2017 17:08
Раздел: Python - Модули - peewee
Запись: xintrea/mytetra_db_mcold/master/base/1494598111j9gdx1kcng/text.html на

Query operators

The following types of comparisons are supported by peewee:




x equals y


x is less than y


x is less than or equal to y


x is greater than y


x is greater than or equal to y


x is not equal to y


x IN y, where y is a list or query


x IS y, where y is None/NULL


x LIKE y where y may contain wildcards


x ILIKE y where y may contain wildcards



Because I ran out of operators to override, there are some additional query operations available as methods:




Wild-card search for substring.


Search for values beginning with prefix.


Search for values ending with suffix.

.between(low, high)

Search for values between low and high.


Regular expression match.


Binary AND.


Binary OR.


IN lookup (identical to <<).


NOT IN lookup.


IS NULL or IS NOT NULL. Accepts boolean param.


Concatenate two strings using ||.

To combine clauses using logical operators, use:






(User.is_active == True) & (User.is_admin == True)

| (pipe)


(User.is_admin) | (User.is_superuser)


NOT (unary negation)

~(User.username << ['foo', 'bar', 'baz'])

Here is how you might use some of these query operators:

# Find the user whose username is "charlie". == 'charlie')

# Find the users whose username is in [charlie, huey, mickey] << ['charlie', 'huey', 'mickey']), 60000))'C'))

Here is how you might combine expressions. Comparisons can be arbitrarily complex.


Note that the actual comparisons are wrapped in parentheses. Python’s operator precedence necessitates that comparisons be wrapped in parentheses.

# Find any users who are active administrations.
  (User.is_admin == True) &
  (User.is_active == True))

# Find any users who are either administrators or super-users.
  (User.is_admin == True) |
  (User.is_superuser == True))

# Find any Tweets by users who are not admins (NOT IN).
admins = == True)
non_admin_tweets =
  ~(Tweet.user << admins))

# Find any users who are not my friends (strangers).
friends =
  User.username << ['charlie', 'huey', 'mickey'])
strangers = << friends))


Although you may be tempted to use python’s inandor and not operators in your query expressions, these will not work. The return value of an in expression is always coerced to a boolean value. Similarly, andor and not all treat their arguments as boolean values and cannot be overloaded.

So just remember:

  • Use << instead of in
  • Use & instead of and
  • Use | instead of or
  • Use ~ instead of not
  • Don’t forget to wrap your comparisons in parentheses when using logical operators.

For more examples, see the Expressions section.


LIKE and ILIKE with SQLite

Because SQLite’s LIKE operation is case-insensitive by default, peewee will use the SQLite GLOB operation for case-sensitive searches. The glob operation uses asterisks for wildcards as opposed to the usual percent-sign. If you are using SQLite and want case-sensitive partial string matching, remember to use asterisks for the wildcard.

Так же в этом разделе:
MyTetra Share v.0.58
Яндекс индекс цитирования