Django Tutorial · QuerySets

QuerySet Introduction

Learn all about QuerySet Introduction in this comprehensive tutorial.

5 min read intermediate
  • A QuerySet is a collection of data from a database.
  • In views.

Django QuerySet

A QuerySet is a collection of data from a database.

A QuerySet is built up as a list of objects.

QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data at an early stage.

In this tutorial we will be querying data from the Member table.

Querying Data

In views.py, we have a view for testing called testing where we will test different queries.

In the example below we use the .all() method to get all the records and fields of the Member model:

The object is placed in a variable called mydata, and is sent to the template via the context object as mymembers, and looks like this:

As you can see, our Member model contains 5 records, and are listed inside the QuerySet as 5 objects.

In the template you can use the mymembers object to generate content:

Module quiz

2 questions
1

Which of the following is true about QuerySet Introduction?

2

What is the most common pitfall when working with QuerySet Introduction?

Answer all questions to submit.