How To Retrieve Model Data Use get_list_or_404 And get_object_or_404 Shortcut Function

Django view is just a python function defined in the Django project / app_name / file. The view function will receive and process user request ( generally get model data from backend database table ) and then return a HttpResponse object with a html template. The model data is passed back to the client as a response context attribute. And then Django will render the html text to the client browser. This article will focus on how to get the backend model data use Django shortcut function get_list_or_404 and get_object_or_404.

1. Django Shortcut Function get_list_or_404, get_object_or_404.

django.shortcuts package contains a lot of helper functions or classes that can help you to develop python MVC application easily. And get_list_or_404,  get_object_or_404 are just the two shortcut function that can retrieve model data from backend database. The two function will return a django.http.response.Http404 error if no match model data has been retrieved. Otherwise get_list_or_404 will return a list of the model class instance, get_object_or_404 will just return one matched model object.

  1. get_list_or_404 ( model_class, filter_field_lookup1, filter_field_lookup2,…… ) : The first paramter can be a model class, QuerySet or a model manager. The follow parameters are the model field lookup filter conditions, you can specify multiple field lookup condition.
  2. get_object_or_404 ( model_class, filter_field_lookup1, filter_field_lookup2,…… ) : The parameters are similar with get_list_or_404.

2. get_list_or_404, get_object_or_404 Example.

To use the shortcut functions you need to import them first. Please see the comments in below example.

# import shortcut functions and model class first.
>>> from dept_emp.models import Department
>>> from django.shortcuts import get_list_or_404, get_object_or_404

>>> get_list_or_404(Department)
[<Department: Develop,Develop web site use Python Django>, <Department: Quality Assurance,Responsible for company website quality and test.>, <Department: Market,Market plan and implement.>]

# if no list return then throw Http404 error.
>>> get_list_or_404(Department, dept_name__contains='Hello')
Traceback (most recent call last):
  File "/usr/lib/python3.6/", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/zhaosong/.local/lib/python3.6/site-packages/django/", line 115, in get_list_or_404
    raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
django.http.response.Http404: No Department matches the given query.

>>> get_list_or_404(Department.objects, dept_name__exact='Develop')
[<Department: Develop,Develop web site use Python Django>]

>>> get_object_or_404(Department, dept_name__contains='Dev')
<Department: Develop,Develop web site use Python Django>

# if more than one model data return then throw MultipleObjectsReturned error
>>> get_object_or_404(Department, dept_name__contains='e')
Traceback (most recent call last):
  File "/usr/lib/python3.6/", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/zhaosong/.local/lib/python3.6/site-packages/django/", line 93, in get_object_or_404
    return queryset.get(*args, **kwargs)
  File "/home/zhaosong/.local/lib/python3.6/site-packages/django/db/models/", line 403, in get
    (self.model._meta.object_name, num)
dept_emp.models.MultipleObjectsReturned: get() returned more than one Department -- it returned 4!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.