Loading lesson path
Overview
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.
id firstname lastname phone joined_date
5551234
Formula
2022 - 01 - 055557777
Formula
2022 - 04 - 015554321
Formula
2021 - 12 - 245551234
Formula
2021 - 05 - 015559876
Formula
2022 - 09 - 29In 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
View views.py
from django.http import HttpResponse from django.template import loader from .models import Member
def testing(request):Formula
mydata = Member.objects.all()
template = loader.get_template('template.html')context = {
'mymembers': mydata,
}
return HttpResponse(template.render(context, request))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: <QuerySet [ <Member: Member object (1)>, <Member: Member object (2)>, <Member: Member object (3)>, <Member: Member object (4)>, <Member: Member object (5)> ]> 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:
Formula
Template templates/template.html<table border='1'> <tr>
Formula
< th > ID </th >
< th > Firstname </th >
< th > Lastname </th ></tr>
{% for x in mymembers %}<tr> <td></td> <td></td> <td></td> </tr>
{% endfor %}</table>