I Didn't Know QuerySets Could Do That

Charlie Guo

Tuesday 5:10 p.m.–5:40 p.m.
Audience level: Intermediate or Advanced

Description

QuerySets are awesome! Django has a great ORM, and the use of QuerySets is a compelling reason to use Django even for smaller projects. But are you getting the most out of your QuerySets and Managers? Let's look at some of the lesser used features of Django's QuerySets, such as F() expressions and Q() objects, the extra() method and raw SQL queries, and extending the base Manager class.

Abstract

QuerySets and object Managers are a core part of Django, and can be extremely powerful. But I didn't always know about some of their more advanced capabilities.

Basic methods

You have likely used filter(), exclude(), and order_by(). You've even probably used an aggregation method like Sum() or Count(). Less common, however, are query(), only()/defer(), and select_related().

F Expressions / Q Objects

For some more complex queries, those basic functions and filters won't cut it. How do you construct a query that needs to check for field A or field B? What do you do if you need to multiply two fields together and then sum them? Look no further than F() and Q().

Raw SQL / The extra() method

As a last resort, it's entirely possible to use raw SQL queries to get the database results that you need. The sky's the limit, but there are definitely downsides to this approach; pitfalls include SQL injections and database backend portability issues.

Managers

A talk on QuerySets would be incomplete without mentioning Managers, and how to leverage Manager customization to make your life easier. Writing methods on existing Managers, and creating custom ones can go a long way towards being DRY and reducing the potential for errors.


Buy your conference and tutorial tickets here! Tutorials are $150 per session.