abelcastro.dev

Mocking APIs with json-server: A Step-by-Step Guide

2023-08-23

Rest-APIChatGPT

The Django n+1 issue and solutions for it

2023-02-09

DjangoChatGPT

Django ORM - call queryset update() in json field when the key to update is not present

2022-05-31

PostgresDjango
1
...
5
6
7
...
9

Abel Castro 2025 - checkout the source code of this page on GitHub - Privacy Policy

Introduction

During the development of web applications, working with APIs is a common task. However, sometimes APIs might not be fully developed or available, which can hinder the development process. To overcome this challenge, developers often resort to mocking APIs to simulate their behavior. One popular tool for achieving this is json-server. In this article, we'll walk you through the process of mocking an API using json-server and provide an example of how to create a mock API with custom data.

Getting Started

  1. Installation: First, ensure you have Node.js installed on your machine. If not, download and install it from the official Node.js website. Once Node.js is installed, open your terminal and install json-server globally using npm:
npm install -g json-server
  1. Create a JSON File: Start by creating a JSON file to hold your mock data. You can use any text editor or IDE to create this file. For example, let's create a file named db.json with the following content:
{
 "products": [
   {
     "id": 1,
     "name": "Mock Product 1",
     "price": 19.99
   },
   {
     "id": 2,
     "name": "Mock Product 2",
     "price": 29.99
   }
 ]
}

Save this file in your project directory.

  1. Start the Mock Server: Open your terminal, navigate to the directory containing db.json, and start the mock server by running:
json-server --watch db.json

The mock server will start at http://localhost:3000.

  1. Accessing the Mock API: You can now access your mock API by sending HTTP requests to the appropriate endpoints. In our case, the data will be available at http://localhost:3000/products.

Customizing the Mock API

The example above showcased a simple mock API for products. You can customize the data and structure of the API according to your application's needs. Add more endpoints, nested data, or different types of data to match your requirements.

Conclusion

json-server is a valuable tool for developers to create mock APIs that simulate real API behavior. It allows you to continue developing frontend components without waiting for backend APIs to be fully implemented. By following the steps outlined in this article, you can quickly set up a mock API for testing and development purposes. This approach enhances productivity and ensures a smooth development process, even when working with incomplete or unavailable APIs. Happy mocking!

Django is a popular and powerful web framework for Python that has become the go-to choice for many web developers. However, one common performance problem that can occur when using Django is the "n+1" issue. This problem can arise when querying a large amount of data, leading to increased loading times and decreased performance. In this blog post, we'll explore what the n+1 issue is, why it occurs, and how to solve it.

What is the n+1 issue in Django?

The n+1 issue occurs when querying a large amount of data from a database. Consider the following example: you have a model that represents a blog post, and you want to retrieve a list of all blog posts along with the author of each post. To do this, you might run the following code:

posts = BlogPost.objects.all()
for post in posts:
    author = post.author

While this code will retrieve all the information you need, it can be slow and inefficient when dealing with large amounts of data. The reason for this is that it will execute a separate SQL query for each post to retrieve the author information. This can lead to an excessive number of SQL queries and slow down your application.

Why does the n+1 issue occur in Django?

The n+1 issue occurs because Django does not retrieve all the data for a relationship in a single query. Instead, it retrieves data for each relationship as it is needed. In the example above, this means that Django will execute a separate SQL query for each post to retrieve the author information. This can lead to a large number of SQL queries and slow down your application.

How to solve the n+1 issue in Django?

There are several ways to solve the n+1 issue in Django, and the best solution depends on your specific use case. Here are some common solutions:

  1. Use the select_related method

The select_related method is a convenient way to retrieve related data in a single query. You can use it to retrieve the author information for all posts in a single query like this:

posts = BlogPost.objects.all().select_related('author')

for post in posts:
author = post.author
  1. Use the prefetch_related method

The prefetch_related method is similar to select_related, but it is more flexible and can be used for more complex relationships. You can use it to retrieve the author information for all posts in a single query like this:

posts = BlogPost.objects.all().prefetch_related('author')

for post in posts:
author = post.author

Conclusion

The n+1 issue is a common performance problem that can occur when using Django. By understanding what the n+1 issue is, why it occurs, and how to solve it, you can ensure that your Django applications run smoothly and efficiently.

Did you liked this article?

This content was generated by ChatGPT at 9th February 2023 with the following questions:

  • Write a blog post about django n+1 issues and solutions for it
  • Generate a markdown file with that content
  • Write a meta description for this post

The original post had a solution number 3 that looked like this:

Use the in operator: The in operator can be used to retrieve multiple records in a single query. You can use it to retrieve the author information for all posts in a single query like this:

posts = BlogPost.objects.all()
author_ids = [post.author_id for post in posts]
authors = Author.objects.filter(id__in=author_ids)

I told ChatGPT that this is not a solution for the issue and it answered this:

"Thank you for pointing that out. You're correct, using the in operator as I described in Solution 3 is not a direct solution for the n+1 issue. It is more of a workaround that can be used to reduce the number of SQL queries, but it doesn't address the root cause of the problem."

I learnt today that this is not possible with the Django ORM to call update in a queryset json field when the key that you want to update is not present. For example:

queryset.update(json_data__key="something")

This will return FieldDoesNotExist: ModelClass has no field named 'json_data__key' because some objects does not have the key “key”.

I found in this stackoverflow post a great solution for this.

from django.db.models.expressions import Func

class JsonSetValue(Func):
    function = "jsonb_set"
    template = "%(function)s(%(expressions)s, '{\"%(keyname)s\"}','\"%(new_value)s\"', %(create_missing)s)"
    arity = 1

    def __init__(
        self,
        expression: str,
        keyname: str,
        new_value: str,
        create_missing: bool = False,
        **extra,
    ):
        super().__init__(
            expression,
            keyname=keyname,
            new_value=new_value,
            create_missing="true" if create_missing else "false",
            **extra,
        )

With help of JsonSetValue you will be able to do the following:

queryset.update(
    json_data=JsonSetValue(
        "json_data",
        keyname="key",
        new_value="something",
        create_missing=True,
    )
)