Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (99 sloc) 7.362 kb
09fe95e @coleifer Update image.
authored
1 .. image:: http://media.charlesleifer.com/blog/photos/p1423749536.32.png
6464df7 @coleifer Adding a logo, thanks majorz!!
authored
2
035b7b5 @coleifer Renaming from weez to peewee
authored
3 peewee
4 ======
4fd56bd @coleifer Initial commit - beware
authored
5
efb8dc1 @coleifer Beefing up docs & readme
authored
6 Peewee is a simple and small ORM. It has few (but expressive) concepts, making it easy to learn and intuitive to use.
7
6ffbc40 @coleifer Readme updates.
authored
8 * A small, expressive ORM
9 * Written in python with support for versions 2.6+ and 3.2+.
10 * built-in support for sqlite, mysql and postgresql
0d99bfb @coleifer README updates.
authored
11 * tons of extensions available in the `playhouse <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html>`_
12
13 * `Postgresql HStore, JSON, arrays and more <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#postgres-ext>`_
14 * `SQLite full-text search, user-defined functions, virtual tables and more <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#sqlite-ext>`_
15 * `Schema migrations <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#migrate>`_ and `model code generator <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#pwiz>`_
16 * `Connection pool <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#pool>`_
17 * `Encryption <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#sqlcipher-ext>`_
18 * `and much, much more... <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html>`_
0faa09f @coleifer Updating the readme and todo
authored
19
efb8dc1 @coleifer Beefing up docs & readme
authored
20 New to peewee? Here is a list of documents you might find most helpful when getting
21 started:
22
0d99bfb @coleifer README updates.
authored
23 * `Quickstart guide <http://docs.peewee-orm.com/en/latest/peewee/quickstart.html#quickstart>`_ -- this guide covers all the essentials. It will take you between 5 and 10 minutes to go through it.
efb8dc1 @coleifer Beefing up docs & readme
authored
24 * `Guide to the various query operators <http://docs.peewee-orm.com/en/latest/peewee/querying.html#query-operators>`_ describes how to construct queries and combine expressions.
25 * `Field types table <http://docs.peewee-orm.com/en/latest/peewee/models.html#field-types-table>`_ lists the various field types peewee supports and the parameters they accept.
5a9386c @coleifer Readme update with travis-ci link.
authored
26
0d99bfb @coleifer README updates.
authored
27 For flask helpers, check out the `flask_utils extension module <http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#flask-utils>`_. You can also use peewee with the popular extension `flask-admin <http://flask-admin.readthedocs.org/en/latest/>`_ to provide a Django-like admin interface for managing peewee models.
974289d @coleifer Updating the readme
authored
28
efb8dc1 @coleifer Beefing up docs & readme
authored
29 Examples
30 --------
31
6ffbc40 @coleifer Readme updates.
authored
32 Defining models is similar to Django or SQLAlchemy::
862945f @coleifer Updating the readme
authored
33
6ffbc40 @coleifer Readme updates.
authored
34 from peewee import *
1f763ac @wwj718 Update README.rst
wwj718 authored
35 import datetime
26e5f90 @coleifer Links to notebook.
authored
36
6ffbc40 @coleifer Readme updates.
authored
37 db = SqliteDatabase('my_database.db', threadlocals=True)
0faa09f @coleifer Updating the readme and todo
authored
38
6ffbc40 @coleifer Readme updates.
authored
39 class BaseModel(Model):
40 class Meta:
41 database = db
35aa4ed @coleifer Whitespace
authored
42
6ffbc40 @coleifer Readme updates.
authored
43 class User(BaseModel):
44 username = CharField(unique=True)
35aa4ed @coleifer Whitespace
authored
45
6ffbc40 @coleifer Readme updates.
authored
46 class Tweet(BaseModel):
47 user = ForeignKeyField(User, related_name='tweets')
48 message = TextField()
49 created_date = DateTimeField(default=datetime.datetime.now)
50 is_published = BooleanField(default=True)
35aa4ed @coleifer Whitespace
authored
51
6ffbc40 @coleifer Readme updates.
authored
52 Connect to the database and create tables::
35aa4ed @coleifer Whitespace
authored
53
6ffbc40 @coleifer Readme updates.
authored
54 db.connect()
55 db.create_tables([User, Tweet])
35aa4ed @coleifer Whitespace
authored
56
6ffbc40 @coleifer Readme updates.
authored
57 Create a few rows::
31fa113 @coleifer Adding notes to readme and a few tests for new python operator queries
authored
58
6ffbc40 @coleifer Readme updates.
authored
59 charlie = User.create(username='charlie')
60 huey = User(username='huey')
61 huey.save()
35aa4ed @coleifer Whitespace
authored
62
6ffbc40 @coleifer Readme updates.
authored
63 # No need to set `is_published` or `created_date` since they
64 # will just use the default values we specified.
65 Tweet.create(user=charlie, message='My first tweet')
66
67 Queries are expressive and composable::
68
69 # A simple query selecting a user.
70 User.get(User.username == 'charles')
31fa113 @coleifer Adding notes to readme and a few tests for new python operator queries
authored
71
6ffbc40 @coleifer Readme updates.
authored
72 # Get tweets created by one of several users. The "<<" operator
73 # corresponds to the SQL "IN" operator.
74 usernames = ['charlie', 'huey', 'mickey']
75 users = User.select().where(User.username << usernames)
76 tweets = Tweet.select().where(Tweet.user << users)
77
78 # We could accomplish the same using a JOIN:
79 tweets = (Tweet
80 .select()
81 .join(User)
82 .where(User.username << usernames))
83
84 # How many tweets were published today?
85 tweets_today = (Tweet
86 .select()
87 .where(
88 (Tweet.created_date >= datetime.date.today()) &
89 (Tweet.is_published == True))
90 .count())
91
92 # Paginate the user table and show me page 3 (users 41-60).
93 User.select().order_by(User.username).paginate(3, 20)
31fa113 @coleifer Adding notes to readme and a few tests for new python operator queries
authored
94
6ffbc40 @coleifer Readme updates.
authored
95 # Order users by the number of tweets they've created:
96 tweet_ct = fn.Count(Tweet.id)
97 users = (User
98 .select(User, tweet_ct.alias('ct'))
8d011cf @coleifer Use JOIN. instead of JOIN_
authored
99 .join(Tweet, JOIN.LEFT_OUTER)
6ffbc40 @coleifer Readme updates.
authored
100 .group_by(User)
101 .order_by(tweet_ct.desc()))
7d1ad9a @coleifer Update README.rst
authored
102
6ffbc40 @coleifer Readme updates.
authored
103 # Do an atomic update
104 Counter.update(count=Counter.count + 1).where(
105 Counter.url == request.url)
106
107 Check out the `example app <http://docs.peewee-orm.com/en/latest/peewee/example.html>`_ for a working Twitter-clone website written with Flask.
7d1ad9a @coleifer Update README.rst
authored
108
5b9055a @coleifer Highlighting the docs and forum in readme
authored
109 Learning more
110 -------------
111
6ffbc40 @coleifer Readme updates.
authored
112 Check the `documentation <http://docs.peewee-orm.com/>`_ for more examples.
7599e63 @coleifer Adding note about irc
authored
113
6ffbc40 @coleifer Readme updates.
authored
114 Specific question? Come hang out in the #peewee channel on freenode.irc.net, or post to the mailing list, http://groups.google.com/group/peewee-orm . If you would like to report a bug, `create a new issue <https://github.com/coleifer/peewee/issues/new>`_ on GitHub.
03a5aa4 @coleifer Updating python version info in readme
authored
115
506ecff @coleifer Important update
authored
116 Still want more info?
117 ---------------------
118
119 .. image:: http://media.charlesleifer.com/blog/photos/wat.jpg
120
efb8dc1 @coleifer Beefing up docs & readme
authored
121 I've written a number of blog posts about building applications and web-services with peewee (and usually Flask). If you'd like to see some real-life applications that use peewee, the following resources may be useful:
122
123 * `Building a note-taking app with Flask and Peewee <http://charlesleifer.com/blog/saturday-morning-hack-a-little-note-taking-app-with-flask/>`_ as well as `Part 2 <http://charlesleifer.com/blog/saturday-morning-hacks-revisiting-the-notes-app/>`_ and `Part 3 <http://charlesleifer.com/blog/saturday-morning-hacks-adding-full-text-search-to-the-flask-note-taking-app/>`_.
124 * `Analytics web service built with Flask and Peewee <http://charlesleifer.com/blog/saturday-morning-hacks-building-an-analytics-app-with-flask/>`_.
125 * `Personalized news digest (with a boolean query parser!) <http://charlesleifer.com/blog/saturday-morning-hack-personalized-news-digest-with-boolean-query-parser/>`_.
126 * `Using peewee to explore CSV files <http://charlesleifer.com/blog/using-peewee-to-explore-csv-files/>`_.
127 * `Structuring Flask apps with Peewee <http://charlesleifer.com/blog/structuring-flask-apps-a-how-to-for-those-coming-from-django/>`_.
128 * `Creating a lastpass clone with Flask and Peewee <http://charlesleifer.com/blog/creating-a-personal-password-manager/>`_.
129 * `Building a web-based encrypted file manager with Flask, peewee and S3 <http://charlesleifer.com/blog/web-based-encrypted-file-storage-using-flask-and-aws/>`_.
130 * `Creating a bookmarking web-service that takes screenshots of your bookmarks <http://charlesleifer.com/blog/building-bookmarking-service-python-and-phantomjs/>`_.
131 * `Building a pastebin, wiki and a bookmarking service using Flask and Peewee <http://charlesleifer.com/blog/dont-sweat-small-stuff-use-flask-blueprints/>`_.
e488ab0 @coleifer Link to sqlcipher post.
authored
132 * `Encrypted databases with Python and SQLCipher <http://charlesleifer.com/blog/encrypted-sqlite-databases-with-python-and-sqlcipher/>`_.
a407501 @coleifer moar linx
authored
133 * `Dear Diary: An Encrypted, Command-Line Diary with Peewee <http://charlesleifer.com/blog/dear-diary-an-encrypted-command-line-diary-with-python/>`_.
134 * `Query Tree Structures in SQLite using Peewee and the Transitive Closure Extension <http://charlesleifer.com/blog/querying-tree-structures-in-sqlite-using-python-and-the-transitive-closure-extension/>`_.
Something went wrong with that request. Please try again.