I want to create a json file from my Django sqlite3 database as a backup rather than displaying it on a webpage

I am able to get the data from my sqlite3 database and project it in a webpage in Json but am having trouble replicating that when wanting to create a file. I am new to python, Django and working with databases. I have been looking around and tried several pieces of code, but the information I could find pertained to what I have already achieved, which is displaying the data on a webpage.

This is how I currently use my views.py file to display the data on a webpage.

import json
from django.utils import timezone
from django.db.models import Q
from django.http import JsonResponse
from django.views import View
from chpapi.models import BlockedList


class IPListJsonView(View):
    def get(self, request):

        json_response = {
            "version": "",
            "description": "",
            "objects": [
                {
                    "name": "",
                    "id": "",
                    "description": "",
                    "ranges": ["2.2.2.2"],
                },
            ],
        }

        group_map = {}

        for ip in BlockedList.objects.filter(
            Q(end_date__isnull=True) | Q(end_date__gte=timezone.now())
        ).select_related("group"):
            group_id = str(ip.group.id)
            if group_id not in group_map:
                group_map[group_id] = {
                    "name": ip.group.name,
                    "id": ip.group.group_id,
                    "description": ip.group.description,
                    "ranges": [],
                }
            group_map[group_id]["ranges"].append(ip.address)

        json_response["objects"] = list(group_map.values())

        return JsonResponse(json_response)

The above code does produce what I want, I have to leave out some objects, but basically the four columns I am grabbing from my models.py are the name, group_id, description and ranges which is an IP value which has its own definition as Django GenericIPAddress did not support certain attributes I wanted.

I thought a simple fix in terms of generating a file as well (This is for safety reasons; in case the database or backup could get corrupted) would be as simple as inserting this code below.

    data = JsonResponse

    with open('test_data.json', 'w') as fp:
        json.dump(data, fp, indent=4)

What ends up happening is that I get a file produced, but it is empty, I am thinking I will have to write a new class in the views for writing a file. If possible if someone has any articles or even a solution in terms of extracting columns in the Sqlite3 database and writing it to a file in the json format that would be helpful. Or if it is possible to include the production of a file in the same views class.

Please let me know if I did not include enough information to go off of and I will edit and improve this post.

New contributor

Steve-o is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật