Creating a JSON file in Directory from sqlite Database

Hello,

So, I am asking this question because I need new sets of eyes to look at this, I will show my current views.py code.

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

        json_response =  {}

        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)

This is how I am currently displaying my information in my database, there is no file created, it is accessible in a URL, it just grabs the groups from the database and displays it in Json format.

path ("ip-json/", IPListJsonView.as_view(), name"IP_list")

Am I just blind and can insert a write file block of code within the current views code to also produce a file. Or do I need to create a whole separate class that will pull data from the database as well and create a file with the most up to date information. Is there any good links that I can visit or any recommendations on how to go at this, I am having issues and at a standstill. I have tried that first thing I mention, inserting code in current block of code, but I can never actually get anything inserted in the file, as in the file is just blank.

Apologies for long message,
Steven

Just in case anyone who runs into this issue, here is the solution.

def create_ip_file():
    try:
        base_dir = "path/" # Just put in the path where you want the folder, I would recommend it for security pruposes, you don't want to create files in the same folder where your code is written.

        filename = "checkpoint_blocklist.json"

        full_path = os.path.join(base_dir, filename)

        with open(full_path, "w") as f:
            f.write(json.dumps(get_ip_list_json(), indent=4))
    except Exception as e:
        print(e)