About The Order of JSON Fields

Since Python 3.6, the standard dict type maintains insertion order. So you can use a simple dictionary with JsonResponse and it will preserve the order of fields:

from django.http.response import JsonResponse

def json_details(request):
    data = {
        "title": "This is a title",
        "description": "This is a description",
        "published": "2020-02-02 02:02",
    }
    return JsonResponse(data)

Previously, you would have had to use OrderedDict for that:

from collections import OrderedDict
from django.http.response import JsonResponse

def json_details(request):
    data = OrderedDict([
        ("title", "This is a title"),
        ("description", "This is a description"),
        ("published", "2020-02-02 02:02"),
    ])
    return JsonResponse(data)

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2 Python 3