Tutorials References Exercises Videos Menu
Create Website Get Certified Upgrade

Django QuerySet Get Data


Get Data

There are different methods to get data from a model into a QuerySet.


The values() Method

The values() method allows you to return each object as a Python dictionary, with the names and values as key/value pairs:

View

members/views.py:

from django.http import HttpResponse
from django.template import loader
from .models import Members

def testing(request):
  mydata = Members.objects.all().values()
  template = loader.get_template('template.html')
  context = {
    'mymembers': mydata,
  }
  return HttpResponse(template.render(context, request))
Run Example »

Return Specific Columns

The values_list() method allows you to return only the columns that you specify.

View

members/views.py:

from django.http import HttpResponse
from django.template import loader
from .models import Members

def testing(request):
  mydata = Members.objects.values_list('firstname')
  template = loader.get_template('template.html')
  context = {
    'mymembers': mydata,
  }
  return HttpResponse(template.render(context, request))
Run Example »

Return Specific Rows

You can filter the search to only return specific rows/records, by using the filter() method.

View

members/views.py:

from django.http import HttpResponse
from django.template import loader
from .models import Members

def testing(request):
  mydata = Members.objects.filter(firstname='Emil').values()
  template = loader.get_template('template.html')
  context = {
    'mymembers': mydata,
  }
  return HttpResponse(template.render(context, request))
Run Example »

You will learn more about the filter() method in the next chapter.