VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-output-django-queryset-as-json/

⇱ How to Output Django QuerySet as JSON - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Output Django QuerySet as JSON

Last Updated : 23 Jul, 2025

In Django, a common task for web developers is to return data in JSON format, especially when working with APIs. A Django QuerySet, which is a collection of database queries, can be serialized and output as JSON. This article will guide us through how to output a Django QuerySet as JSON using two methods:

Step By Step Guide to Output Django QuerySet as JSON

Before diving into the examples, let’s set up a Django project to demonstrate how to output QuerySet as JSON. If you already have a Django project, you can skip this step.

Step 1: Install Django

First, ensure that Django is installed:

pip install django

Step 2: Create a New Django Project

Create a new project and app:

django-admin startproject myproject
cd myproject
python manage.py startapp myapp

Add myapp to the INSTALLED_APPS in settings.py file

Step 3: Define a Django Model

In myapp/models.py, create a simple model:

Step 4: Create and Apply Migrations

Run migrations to set up the database:

python manage.py makemigrations
python manage.py migrate

Step 5: Add Some Data

We can add some sample data using Django’s shell:

python manage.py shell

>>>from myapp.models import Book
>>>Book.objects.create(title="The Great Gatsby", author="F. Scott Fitzgerald", published_date="1925-04-10")
>>>Book.objects.create(title="To Kill a Mockingbird", author="Harper Lee", published_date="1960-07-11")

Now that we have some data in the database, let's move on to outputting a QuerySet as JSON.

👁 orm-
Using Django Shell

1. Serialize Django QuerySet using Serializer

Django’s serializers module can be used to serialize a QuerySet to JSON.

Step 1: Define the View

In myapp/views.py, we’ll create a view that serializes a QuerySet to JSON:

  • We use serializers.serialize() to convert the QuerySet into JSON format.
  • The HttpResponse() sends the serialized JSON as the response, with content_type='application/json' to specify the content type.

Note: The serializers.serialize() method takes a queryset not a single object and returns data in the valid defined format.

Step 2: Set up the URL

In urls.py, define the URL for the book_list view:

Run the Server

python manage.py runserver

If we access the /books/ URL, we’ll get a response similar to:

👁 serializers
Json response in Django

2. Serialize Django QuerySet using JsonResponse

Another method to output JSON in Django is by using the JsonResponse class, which automatically converts Python dictionaries into JSON format.

Step 1: Define the View with JsonResponse

In myapp/views.py, create a new view using JsonResponse:

  • We use the .values() method to retrieve a QuerySet of dictionaries containing only the fields we need (title, author, published_date).
  • JsonResponse automatically serializes the Python list into JSON format.
  • The safe=False parameter is required because by default, JsonResponse only accepts dictionaries, not lists.

Step 2: Set up the URL

In urls.py, define the URL for the book_list_json view:

Run the Server

python manage.py runserver

If we access the /books-json/ URL, we’ll get a response like:

👁 second
Using JSONResponse to send data in Django

Conclusion

In this article, we covered two methods to output Django QuerySets as JSON:

  1. Using Django’s serializers: This method provides a full representation of the model, including metadata like the model name and primary key.
  2. Using JsonResponse: A simpler approach that directly outputs the QuerySet as a list of dictionaries, suitable for lightweight API responses.

Both methods are useful, depending on the specific requirements of our project. If we need complete control over how the data is structured, JsonResponse is a flexible option, whereas serialization is more suitable for cases where model metadata is important.

Comment
Article Tags: