Skip to content

Commit

Permalink
Updating the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Oct 8, 2012
1 parent 6a5b2f3 commit 862945f
Showing 1 changed file with 32 additions and 72 deletions.
104 changes: 32 additions & 72 deletions README.rst
Expand Up @@ -13,57 +13,41 @@ peewee
For flask integration, including an admin interface and RESTful API, check For flask integration, including an admin interface and RESTful API, check
out `flask-peewee <https://github.com/coleifer/flask-peewee/>`_. out `flask-peewee <https://github.com/coleifer/flask-peewee/>`_.


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>`_.

Examples:: Examples::


# a simple query selecting a user # a simple query selecting a user
User.get(username='charles') User.get(User.username == 'charles')

# get the staff and super users # get the staff and super users
editors = User.select().where(Q(is_staff=True) | Q(is_superuser=True)) editors = User.select().where(

(User.is_staff == True) |
# get tweets by editors (User.is_superuser == True)
Tweet.select().where(user__in=editors) )

# get tweets by editors ("<<" maps to IN)
Tweet.select().where(Tweet.user << editors)
# how many active users are there? # how many active users are there?
User.select().where(active=True).count() User.select().where(User.active == True).count()

# paginate the user table and show me page 3 (users 41-60) # paginate the user table and show me page 3 (users 41-60)
User.select().order_by(('username', 'asc')).paginate(3, 20) User.select().order_by(User.username).paginate(3, 20)

# order users by number of tweets # order users by number of tweets
User.select().annotate(Tweet).order_by(('count', 'desc')) User.select().annotate(Tweet).order_by(

fn.Count(Tweet.id).desc()
# another way of expressing the same )
User.select({
User: ['*'],
Tweet: [Count('id', 'count')]
}).group_by('id').join(Tweet).order_by(('count', 'desc'))


You can use django-style syntax to create select queries::

# how many active users are there?
User.filter(active=True).count()

# get tweets by a specific user
Tweet.filter(user__username='charlie')

# get tweets by editors
Tweet.filter(Q(user__is_staff=True) | Q(user__is_superuser=True))


You can use python operators to create select queries::

# how many active users are there?
User.select().where(User.active == True).count()

# get me all users in their thirties
User.select().where((User.age >= 30) & (User.age < 40))


# get me tweets from today by active users # a similar way of expressing the same
Tweet.select().join(User).where( User.select(
(Tweet.pub_date >= today) & User, fn.Count(Tweet.id).alias('ct')
(User.active == True) ).join(Tweet).group_by(User).order_by(R('ct desc'))
# do an atomic update
Counter.update(count=Counter.count + 1).where(
Counter.url == request.url
) )




Expand Down Expand Up @@ -159,43 +143,19 @@ queries come in 4 flavors (select/update/insert/delete).
there's the notion of a *query context* which is the model being selected there's the notion of a *query context* which is the model being selected
or joined on:: or joined on::


User.select().where(active=True).order_by(('username', 'asc')) User.select().where(User.active == True).order_by(User.username)


since User is the model being selected, the where clause and the order_by will since User is the model being selected, the where clause and the order_by will
pertain to attributes on the User model. User is the current query context pertain to attributes on the User model. User is the current query context
when the .where() and .order_by() are evaluated. when the .where() and .order_by() are evaluated.


an example using joins:: an example using joins::


Tweet.select().where(deleted=False).order_by(('pub_date', 'desc')).join( Tweet.select().join(User).where(
User (Tweet.deleted == False) & (User.active == True)
).where(active=True) ).order_by(Tweet.pub_date.desc())

this will select non-deleted tweets from active users. the first .where() and
.order_by() occur when Tweet is the current *query context*. As soon as the
join is evaluated, User becomes the *query context* and so the following
where() pertains to the User model.


now with q objects
------------------

for users familiar with django's orm, I've implemented OR queries and complex
query nesting using similar notation::

User.select().where(
Q(is_superuser = True) |
Q(is_staff = True)
)

SomeModel.select().where(
(Q(a='A') | Q(b='B')) &
(Q(c='C') | Q(d='D'))
)


# generates something like: this will select non-deleted tweets from active users.
# SELECT * FROM some_obj
# WHERE ((a = "A" OR b = "B") AND (c = "C" OR d = "D"))




using sqlite using sqlite
Expand Down

0 comments on commit 862945f

Please sign in to comment.