Skip to content
This repository
Browse code

Updating the readme

  • Loading branch information...
commit 862945f908923eeceebcb9186db8e650331001d0 1 parent 6a5b2f3
Charles Leifer authored October 08, 2012

Showing 1 changed file with 32 additions and 72 deletions. Show diff stats Hide diff stats

  1. 104  README.rst
104  README.rst
Source Rendered
@@ -13,57 +13,41 @@ peewee
13 13
 For flask integration, including an admin interface and RESTful API, check
14 14
 out `flask-peewee <https://github.com/coleifer/flask-peewee/>`_.
15 15
 
  16
+For notes on the upgrade from 1.0 to 2.0, check out `the upgrade docs <http://peewee.readthedocs.org/en/latest/peewee/upgrading.html>`_.
  17
+
16 18
 Examples::
17 19
 
18 20
     # a simple query selecting a user
19  
-    User.get(username='charles')
20  
-
  21
+    User.get(User.username == 'charles')
  22
+    
21 23
     # get the staff and super users
22  
-    editors = User.select().where(Q(is_staff=True) | Q(is_superuser=True))
23  
-
24  
-    # get tweets by editors
25  
-    Tweet.select().where(user__in=editors)
26  
-
  24
+    editors = User.select().where(
  25
+        (User.is_staff == True) |
  26
+        (User.is_superuser == True)
  27
+    )
  28
+    
  29
+    # get tweets by editors ("<<" maps to IN)
  30
+    Tweet.select().where(Tweet.user << editors)
  31
+    
27 32
     # how many active users are there?
28  
-    User.select().where(active=True).count()
29  
-
  33
+    User.select().where(User.active == True).count()
  34
+    
30 35
     # paginate the user table and show me page 3 (users 41-60)
31  
-    User.select().order_by(('username', 'asc')).paginate(3, 20)
32  
-
  36
+    User.select().order_by(User.username).paginate(3, 20)
  37
+    
33 38
     # order users by number of tweets
34  
-    User.select().annotate(Tweet).order_by(('count', 'desc'))
35  
-
36  
-    # another way of expressing the same
37  
-    User.select({
38  
-        User: ['*'],
39  
-        Tweet: [Count('id', 'count')]
40  
-    }).group_by('id').join(Tweet).order_by(('count', 'desc'))
41  
-
42  
-
43  
-You can use django-style syntax to create select queries::
44  
-
45  
-    # how many active users are there?
46  
-    User.filter(active=True).count()
47  
-
48  
-    # get tweets by a specific user
49  
-    Tweet.filter(user__username='charlie')
50  
-
51  
-    # get tweets by editors
52  
-    Tweet.filter(Q(user__is_staff=True) | Q(user__is_superuser=True))
53  
-
54  
-
55  
-You can use python operators to create select queries::
56  
-
57  
-    # how many active users are there?
58  
-    User.select().where(User.active == True).count()
59  
-
60  
-    # get me all users in their thirties
61  
-    User.select().where((User.age >= 30) & (User.age < 40))
  39
+    User.select().annotate(Tweet).order_by(
  40
+        fn.Count(Tweet.id).desc()
  41
+    )
62 42
 
63  
-    # get me tweets from today by active users
64  
-    Tweet.select().join(User).where(
65  
-        (Tweet.pub_date >= today) &
66  
-        (User.active == True)
  43
+    # a similar way of expressing the same
  44
+    User.select(
  45
+        User, fn.Count(Tweet.id).alias('ct')
  46
+    ).join(Tweet).group_by(User).order_by(R('ct desc'))
  47
+    
  48
+    # do an atomic update
  49
+    Counter.update(count=Counter.count + 1).where(
  50
+        Counter.url == request.url
67 51
     )
68 52
 
69 53
 
@@ -159,7 +143,7 @@ queries come in 4 flavors (select/update/insert/delete).
159 143
 there's the notion of a *query context* which is the model being selected
160 144
 or joined on::
161 145
 
162  
-    User.select().where(active=True).order_by(('username', 'asc'))
  146
+    User.select().where(User.active == True).order_by(User.username)
163 147
 
164 148
 since User is the model being selected, the where clause and the order_by will
165 149
 pertain to attributes on the User model.  User is the current query context
@@ -167,35 +151,11 @@ when the .where() and .order_by() are evaluated.
167 151
 
168 152
 an example using joins::
169 153
 
170  
-    Tweet.select().where(deleted=False).order_by(('pub_date', 'desc')).join(
171  
-        User
172  
-    ).where(active=True)
173  
-
174  
-this will select non-deleted tweets from active users.  the first .where() and
175  
-.order_by() occur when Tweet is the current *query context*.  As soon as the
176  
-join is evaluated, User becomes the *query context* and so the following
177  
-where() pertains to the User model.
178  
-
179  
-
180  
-now with q objects
181  
-------------------
182  
-
183  
-for users familiar with django's orm, I've implemented OR queries and complex
184  
-query nesting using similar notation::
185  
-
186  
-    User.select().where(
187  
-        Q(is_superuser = True) |
188  
-        Q(is_staff = True)
189  
-    )
190  
-
191  
-    SomeModel.select().where(
192  
-        (Q(a='A') | Q(b='B')) &
193  
-        (Q(c='C') | Q(d='D'))
194  
-    )
  154
+    Tweet.select().join(User).where(
  155
+        (Tweet.deleted == False) & (User.active == True)
  156
+    ).order_by(Tweet.pub_date.desc())
195 157
 
196  
-    # generates something like:
197  
-    # SELECT * FROM some_obj
198  
-    # WHERE ((a = "A" OR b = "B") AND (c = "C" OR d = "D"))
  158
+this will select non-deleted tweets from active users.
199 159
 
200 160
 
201 161
 using sqlite

0 notes on commit 862945f

Please sign in to comment.
Something went wrong with that request. Please try again.