<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Linh Fish Dev]]></title><description><![CDATA[Linh Fish Dev]]></description><link>https://linhfishdev.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 13:38:55 GMT</lastBuildDate><atom:link href="https://linhfishdev.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Django in Production #20: Series Wrap-Up — Operations Runbook, Incident Checklist, and What's Next]]></title><description><![CDATA[This final episode wraps up the journey of taking the expense tracker to production: by the end, you will have a complete operations runbook, an incident…]]></description><link>https://linhfishdev.hashnode.dev/django-in-production-20-series-wrap-up-operations-runbook-incident-checklist-and-what-s-next</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-in-production-20-series-wrap-up-operations-runbook-incident-checklist-and-what-s-next</guid><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Tue, 17 Nov 2026 15:18:24 GMT</pubDate><content:encoded><![CDATA[<p>This final episode wraps up the journey of taking the expense tracker to production: by the end, you will have a complete operations runbook, an incident checklist you can use on your very next on-call shift, and a clear upgrade path. Following episode 19 on scaling with more workers and a read replica, this episode pulls the whole series together into a sustainable operations routine.</p>
<h2>What an Operations Runbook Is and What It Contains</h2>
<p>A runbook is a living document that records every operations task: who does what, in which order, when deploying, when something breaks, and when you need to scale. A good runbook has four parts: the standard deploy procedure, how monitoring and alerting work, the backup and restore procedure, and an incident checklist by severity. Keep the runbook in the repo and review it every quarter, or after every major incident.</p>
<h2>Incident Checklist by Severity</h2>
<p>When an incident happens, the first job is not to fix code — it is to determine the severity. A P1 incident means the site is completely down: check the <code>/health</code> endpoint, see whether Gunicorn is still running, check the PostgreSQL connection, and if a deploy just finished, roll back immediately instead of debugging on production. A P2 incident means the system is slow: look at the slow query log, check the Redis hit rate, and see whether the Celery queue is backed up. A P3 incident is an isolated error: read the logs in Sentry and work out whether the error repeats in a pattern. Record every incident with its time, symptoms, cause, and fix, so next time you only need to open the record.</p>
<pre><code>#!/bin/bash
# scripts/incident-triage.sh — chạy khi site có dấu hiệu bất thường
set -e
curl -fsS https://example.com/health/ || echo "ALERT: health endpoint khong phan hoi"
pg_isready -h db.example.com -p 5432 || echo "ALERT: PostgreSQL khong san sang"
redis-cli -h cache.example.com ping || echo "ALERT: Redis khong phan hoi"
systemctl is-active gunicorn celery || echo "ALERT: service dung"
</code></pre>
<h2>What’s Next on the Upgrade Path</h2>
<p>Once the runbook is stable, the three most worthwhile investments are Kubernetes, observability, and deeper automation. Kubernetes replaces manual deploys when you have many services, need pods to recover on their own, and want to scale with load. Observability takes the monitoring from episode 8 to the next level: distributed tracing with OpenTelemetry, structured logs, and one unified dashboard for Django, Celery, and Nginx. Finally, automate the runbook itself: turn the manual checklist into a check script, then into automated alerts. Only take each upgrade step when there is a real need for it.</p>
<pre><code># core/management/commands/ops_status.py
from django.core.management.base import BaseCommand
from django.db import connections
from django.core.cache import cache

class Command(BaseCommand):
    help = "Kiem tra nhanh DB va cache truoc khi deploy"

    def handle(self, *args, **options):
        with connections["default"].cursor() as cursor:
            cursor.execute("SELECT 1")
            cursor.fetchone()
        self.stdout.write(self.style.SUCCESS("PostgreSQL: OK"))
        cache.set("ops_probe", "ok", timeout=10)
        assert cache.get("ops_probe") == "ok"
        self.stdout.write(self.style.SUCCESS("Redis cache: OK"))
</code></pre>
<h2>Apply It to Your Project</h2>
<p>Spend this afternoon writing your first runbook for the expense tracker: start from your current deploy procedure, write down every command you now run by hand, then add the P1, P2, and P3 checklists using the template above. A good production system is not one that never fails — it is one where every failure is handled faster than the last.</p>
<h2>Related Posts</h2>
<ul>
<li><p><a href="https://linhfish.dev/en/2026/11/15/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-actually-need-to-scale/">Django in Production #19: Scaling — More Workers, Load Balancing, Read Replicas, and When You Actually Need to Scale</a></p>
</li>
<li><p><a href="https://linhfish.dev/en/2026/11/14/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts/">Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts</a></p>
</li>
<li><p><a href="https://linhfish.dev/en/2026/11/13/django-in-production-17-ci-cd-with-github-actions-automated-tests-then-deploy/">Django in Production #17: CI/CD with GitHub Actions — Automated Tests, Then Deploy</a></p>
</li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>How is a runbook different from system documentation?</h3>
<p>A runbook focuses on what to do when deploying or when something breaks, while system documentation describes the architecture and the reasons behind design decisions. When you are on call at 2 a.m. during an incident, you need the runbook, not an architecture diagram.</p>
<h3>When should you move to Kubernetes?</h3>
<p>When you have many services to manage together, need pods to recover on their own when they fail, or need to scale with load in ways systemd and manual deploys cannot handle. For a single Django app, Gunicorn plus systemd is still good enough.</p>
<h3>How short should an incident checklist be?</h3>
<p>Short enough to read through in five minutes under stress — usually five to seven steps per severity level. Put the longer detail in the explanation below each step.</p>
<h2>References</h2>
<ul>
<li><p><a href="https://docs.djangoproject.com/en/stable/howto/deployment/checklist/">Django Documentation — Deployment checklist</a></p>
</li>
<li><p><a href="https://docs.djangoproject.com/en/stable/topics/logging/">Django Documentation — Logging</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django in Production #19: Scaling — More Workers, Load Balancing, Read Replicas, and When You Actually Need to Scale]]></title><description><![CDATA[Episode 18 showed you how to deploy with zero downtime, but a single server running 3 Gunicorn workers will hit its limit sooner or later: CPU saturates…]]></description><link>https://linhfishdev.hashnode.dev/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-actually-need-to-scale</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-in-production-19-scaling-more-workers-load-balancing-read-replicas-and-when-you-actually-need-to-scale</guid><category><![CDATA[Django]]></category><category><![CDATA[Python]]></category><category><![CDATA[Devops]]></category><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Mon, 16 Nov 2026 15:20:53 GMT</pubDate><content:encoded><![CDATA[<p>Episode 18 showed you how to deploy with zero downtime, but a single server running 3 Gunicorn workers will hit its limit sooner or later: CPU saturates, requests queue up, and the statistics page gets slower and slower. This episode shows how to scale one step at a time — add workers, balance the load with Nginx, split off a read replica for PostgreSQL — and the signs that tell you when scaling is actually needed.</p>
<h2>Step 1: Add Workers Before Adding Machines</h2>
<p>Before buying another server, squeeze everything out of the one you have. The classic Gunicorn formula is two times the CPU count plus one, but that is only a starting point — measure the real CPU usage and response latency. If CPU still has headroom but requests are slow, the problem is usually a bad query or missing cache — go back to episode 12 and episode 9 before scaling.</p>
<pre><code># Check the real load before deciding to scale
uptime
curl -s http://localhost:8000/health/ -w "\n%{time_total}s\n"
</code></pre>
<h2>Step 2: Load Balancing with Nginx</h2>
<p>When one machine is no longer enough, run the app on two machines and let Nginx spread requests across them. Nginx has been the reverse proxy since episode 3 — now you only add servers to the upstream and pick a load distribution algorithm. <code>least_conn</code> sends each request to the machine with the fewest active connections, which suits heavy requests such as monthly statistics. One important note: the app must be stateless — sessions and file uploads must not live on local disk.</p>
<pre><code>upstream expense_app {
    least_conn;
    server 10.0.0.11:8000;
    server 10.0.0.12:8000;
}

server {
    listen 80;
    server_name chi-tieu.example.com;
    location / {
        proxy_pass http://expense_app;
        proxy_set_header Host $host;
    }
}
</code></pre>
<h2>Step 3: A Read Replica for PostgreSQL</h2>
<p>Most apps read far more than they write: transaction lists, monthly statistics, and reports are all reads. Split off a read replica to carry the read queries, while the primary database only handles writes. Django supports multiple databases out of the box: declare an extra <code>replica</code> in <code>DATABASES</code>, then use a small router to send reads to the replica while writes still go to the primary.</p>
<pre><code># settings.py
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "expense",
        "HOST": "db-primary.internal",
    },
    "replica": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "expense",
        "HOST": "db-replica.internal",
    },
}
DATABASE_ROUTERS = ["expense.routers.ReadReplicaRouter"]
</code></pre>
<pre><code># expense/routers.py
class ReadReplicaRouter:
    def db_for_read(self, model, **hints):
        return "replica"

    def db_for_write(self, model, **hints):
        return "default"

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return db == "default"
</code></pre>
<h2>When Do You Actually Need to Scale?</h2>
<p>Do not scale on a feeling. Scale when at least one clear signal persists for several days: CPU above 70% during peak hours, p95 latency double the normal level, or the database connection pool running dry even with PgBouncer from episode 4. Conversely, if errors only appear in one short window, treat it as unusual traffic or a single slow query. Every scaling step adds one more thing to monitor — only expand when the numbers from episode 8 prove you have hit the ceiling.</p>
<h2>Apply It to Your Project</h2>
<p>For the expense tracker app, start by measuring: record CPU, p95 latency, and database connections for one week. If CPU stays under 50%, do nothing yet — optimize slow queries first. When you need more, add workers, then move to two-machine load balancing; only split off a read replica when read queries are actually hammering the primary database.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/en/2026/11/14/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts/">Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts</a></li>
<li><a href="https://linhfish.dev/en/2026/10/31/django-production-operations-4-postgresql-in-production-connections-conn_max_age-pgbouncer-pooling-basic-backups/">Django Production Operations #4: PostgreSQL in Production — Connections, CONN_MAX_AGE, PgBouncer Pooling, Basic Backups</a></li>
<li><a href="https://linhfish.dev/en/2026/11/05/django-running-in-production-9-caching-with-redis-cache-framework-invalidation-patterns-and-avoiding-stale-data/">Django Running in Production #9: Caching with Redis — Cache Framework, Invalidation Patterns, and Avoiding Stale Data</a></li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>How many Gunicorn workers are enough?</h3>
<p>Start with two times the CPU count plus one, then measure the real numbers. Adding workers only helps when CPU still has headroom; if the database or a query is the bottleneck, more workers only make everything slower.</p>
<h3>Does a read replica serve stale data?</h3>
<p>Yes — replicas lag by tens of milliseconds to a few seconds. Never read freshly written data from a replica: after creating a new transaction, read it back from the primary database, or use <code>using("default")</code>.</p>
<h3>Does load balancing need sticky sessions?</h3>
<p>No, as long as the app is stateless: sessions stored in Redis or the database, files on S3. Sticky sessions are only a temporary patch when the app still keeps state on local disk.</p>
<h2>References</h2>
<ul>
<li><a href="https://docs.djangoproject.com/en/stable/topics/db/multi-db/">Django docs — Multiple databases</a></li>
<li><a href="https://nginx.org/en/docs/http/load_balancing.html">Nginx — Using nginx as HTTP load balancer</a></li>
<li><a href="https://docs.gunicorn.org/en/stable/design.html">Gunicorn docs — design and worker processes</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django in Production #18: Zero-Downtime Deploys — Blue-Green/Rolling, Migration Order and Restarts]]></title><description><![CDATA[Episode 17 built the CI/CD pipeline: push code to main, tests run, then the deploy goes to the server automatically. But an automatic deploy isn't…]]></description><link>https://linhfishdev.hashnode.dev/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-in-production-18-zero-downtime-deploys-blue-green-rolling-migration-order-and-restarts</guid><category><![CDATA[Python]]></category><category><![CDATA[Devops]]></category><category><![CDATA[Django]]></category><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Sun, 15 Nov 2026 15:18:57 GMT</pubDate><content:encoded><![CDATA[<p>Episode 17 built the CI/CD pipeline: push code to main, tests run, then the deploy goes to the server automatically. But an automatic deploy isn't necessarily a zero-downtime one — restart the app while a user is entering an expense and their request fails with a 502. This episode finishes the last step: shipping the new version without users noticing, and rolling back in seconds.</p>
<h2>Why Stop-and-Restart Deploys Cause Downtime</h2>
<p>The simplest deploy is: stop the service, pull the new code, run migrations, then start again. In the gap between stopping and the new app being ready, every request fails — with Gunicorn, usually 5–30 seconds. A zero-downtime deploy removes that gap by always keeping at least one version of the app running.</p>
<h2>The Golden Rule: Migrations First, Code Second</h2>
<p>The correct order for every deploy is: pull the new code, run migrations, then restart the app. Migrations must be backward compatible — old code still runs on the new schema — following the episode 6 pattern: add a nullable column first, deploy the new code, then clean up the old column next time. Do it the other way around and the new app will crash on the old schema immediately.</p>
<pre><code>#!/bin/bash
set -e
cd ~/expense-app
git pull origin main
python manage.py migrate --noinput
python manage.py collectstatic --noinput
sudo systemctl reload gunicorn
curl -fsS http://localhost:8000/health/
</code></pre>
<h2>Rolling Deploys with Gunicorn and systemd</h2>
<p>A rolling deploy is the simplest approach: replace workers one at a time instead of stopping the whole app. Gunicorn supports graceful reload — send the HUP signal, the master process starts new workers running the new code, while old workers finish their in-flight requests before shutting down. The systemd config below uses exactly this mechanism, so a deploy is just one reload command — good enough for a single-server expense tracker.</p>
<pre><code>[Service]
ExecStart=/home/linh/expense-app/venv/bin/gunicorn expense.wsgi:application --bind 127.0.0.1:8000 --workers 3
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=30
</code></pre>
<h2>Blue-Green Deploys with Nginx</h2>
<p>When you need more certainty — for example a big change, or an instant rollback — use blue-green: run two versions of the app side by side on two ports, with Nginx pointing only at the live one. Deploy the new version to the green port, verify it, then switch the upstream to green; the old version keeps running untouched, so rollback is just switching the upstream back. It costs double the resources, but in return you get a rewind button that works in seconds.</p>
<pre><code>upstream expense_app {
    server 127.0.0.1:8001;  # blue: ban dang chay
    # server 127.0.0.1:8002;  # green: ban moi, bat len khi deploy
}

server {
    listen 80;
    server_name chi-tieu.example.com;
    location / {
        proxy_pass http://expense_app;
        proxy_set_header Host $host;
    }
}
</code></pre>
<h2>Health Checks and Rollback</h2>
<p>Rolling or blue-green, never send traffic to the new version before confirming it's alive. After the reload or upstream switch, call the /health/ endpoint from episode 8 and wait until it returns 200. If the health check fails, stop and keep the old version.</p>
<p>A fast rollback is the other half of a zero-downtime deploy. With rolling, keeping the old code and reloading back is enough; with blue-green, just switch the Nginx upstream back to the old version and reload Nginx. Note: if a migration already dropped a column the old code still uses, the rollback will fail — so always drop columns in a separate deploy, after the new code has been running fine.</p>
<h2>Apply It to Your Project</h2>
<p>For the expense tracker, start with a rolling deploy: add ExecReload to the Gunicorn service, then change the episode 17 deploy script from restart to reload — one line of change and the downtime is gone. As users grow, move to blue-green with ports 8001 and 8002, and write the process into a runbook.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/en/2026/11/13/django-in-production-17-ci-cd-with-github-actions-automated-tests-then-deploy/">Django in Production #17: CI/CD with GitHub Actions — Automated Tests, Then Deploy</a></li>
<li><a href="https://linhfish.dev/en/2026/11/02/django-production-operations-6-safe-migrations-in-production-backward-compatible-patterns-zero-downtime-deploys/">Django Production Operations #6: Safe Migrations in Production — Backward-Compatible Patterns, Zero-Downtime Deploys</a></li>
<li><a href="https://linhfish.dev/en/2026/11/12/django-in-production-16-docker-for-production-multi-stage-builds-non-root-runtime-healthchecks/">Django in Production #16: Docker for Production — Multi-Stage Builds, Non-Root Runtime, Healthchecks</a></li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>How is a rolling deploy different from blue-green?</h3>
<p>Rolling replaces workers one at a time on the same app — simple and enough for one server. Blue-green runs two complete versions side by side and switches traffic with Nginx — instant rollback, but it costs double the resources during the switch.</p>
<h3>When should migrations run for zero downtime?</h3>
<p>Before the restart or reload, and migrations must be backward compatible: old code still runs on the new schema. Add a nullable column this time, clean up the old column next time.</p>
<h3>What if the health check fails?</h3>
<p>Stop the deploy immediately and keep the old version. With rolling, the old workers are still serving, so users are unaffected; with blue-green, just don't switch the upstream. Then read the new version's logs, fix the bug, and deploy again.</p>
<h2>References</h2>
<ul>
<li><a href="https://docs.gunicorn.org">Gunicorn — docs.gunicorn.org</a></li>
<li><a href="https://docs.djangoproject.com">Django deployment checklist — docs.djangoproject.com</a></li>
<li><a href="https://nginx.org">Nginx ngx_http_upstream_module — nginx.org</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django: Use auto_now and auto_now_add Correctly — When to Switch to default=timezone.now]]></title><description><![CDATA[Most Django models have two timestamp fields: created_at to know when a record was created and updated_at for the last edit. The quickest way is…]]></description><link>https://linhfishdev.hashnode.dev/django-use-auto-now-and-auto-now-add-correctly-when-to-switch-to-default-timezone-now</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-use-auto-now-and-auto-now-add-correctly-when-to-switch-to-default-timezone-now</guid><category><![CDATA[Python]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Django]]></category><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Sat, 14 Nov 2026 15:18:11 GMT</pubDate><content:encoded><![CDATA[<p>Most Django models have two timestamp fields: created_at to know when a record was created and updated_at for the last edit. The quickest way is auto_now_add=True for created_at and auto_now=True for updated_at — Django fills in both and you never touch them. But these two options have three common "traps": you can't assign an old date when importing data, bulk update() doesn't change updated_at, and the fields are hidden from forms. This post walks through each trap and how to handle it.</p>
<h2>The Problem</h2>
<p>Suppose you import 500 old posts from another blog and want to keep their original publish dates. You write:</p>
<pre><code class="language-python">from datetime import datetime
from django.utils import timezone

old_date = timezone.make_aware(datetime(2024, 5, 10, 9, 0))

# Muốn giữ ngày đăng gốc khi import...
post = Post.objects.create(title="Bài cũ", created_at=old_date)
print(post.created_at)  # Hôm nay, không phải 10/05/2024!
</code></pre>
<p>Result: created_at is overwritten with the current time and the original date is lost. Reason: auto_now_add only runs on INSERT — any value you pass to create() is replaced with timezone.now().</p>
<p>The second trap is in bulk updates. You change the status of 1,000 posts:</p>
<pre><code class="language-python">from django.utils import timezone

# Bẫy thứ hai: cập nhật hàng loạt
Post.objects.filter(status="draft").update(status="published")
# updated_at của 1.000 bài viết vẫn giữ nguyên!
</code></pre>
<p>The updated_at of all posts stays unchanged — because QuerySet.update() runs SQL directly, without calling save(), so auto_now never fires. Users looking at "last updated" will see an old date even though the content just changed.</p>
<h2>The Solution</h2>
<p>To keep the original date when importing, don't use auto_now_add for created_at. Use default=timezone.now instead — note you pass the function, not call it:</p>
<pre><code class="language-python">from django.db import models
from django.utils import timezone

class Post(models.Model):
    title = models.CharField(max_length=200)
    status = models.CharField(max_length=20, default="draft")
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)
</code></pre>
<p>With this approach, creating a record without passing created_at still auto-fills the current time, while importing lets you pass the old date and keep it. These two fields remain editable in the admin and ModelForm, unlike auto_now/auto_now_add, which are hidden.</p>
<p>For bulk updates, pass updated_at explicitly:</p>
<pre><code class="language-python">from django.utils import timezone

Post.objects.filter(status="draft").update(
    status="published",
    updated_at=timezone.now(),
)
</code></pre>
<p>If you still want to keep auto_now_add because you never need to edit created_at, you can — just assign the old date after creating: create the record first, assign created_at, then save() again. Since auto_now_add only runs on INSERT, the second save() doesn't overwrite it.</p>
<pre><code class="language-python">post = Post.objects.create(title="Bài cũ")
post.created_at = old_date  # Gán sau khi tạo thì giữ được
post.save()
</code></pre>
<h2>Apply It to Your Project</h2>
<p>Quick rule: use auto_now_add/auto_now for tables that only need a time "stamp", like logs, activity history, or posts whose date you never edit. Use default=timezone.now when the date is business data — post publish date, expiry date, event start date — because sooner or later you'll need to edit it by hand, import old data, or let users change it in the admin. And after every QuerySet.update() or bulk_update(), check whether updated_at needs an explicit update.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/2026/09/30/django-sua-loi-n1-query-bang-select_related-trong-5-phut/">Django: sửa lỗi N+1 query bằng select_related trong 5 phút</a></li>
<li><a href="https://linhfish.dev/2026/10/12/django-dung-get_or_create-va-update_or_create-dung-cach-lay-hay-tao-ban-ghi-chi-trong-1-dong/">Django: dùng get_or_create() và update_or_create() đúng cách — lấy hay tạo bản ghi chỉ trong 1 dòng</a></li>
<li><a href="https://linhfish.dev/2026/09/30/django-tao-va-cap-nhat-hang-loat-voi-bulk_create-bulk_update-thay-vi-save-trong-vong-lap/">Django: tạo và cập nhật hàng loạt với bulk_create/bulk_update thay vì save() trong vòng lặp</a></li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>How do auto_now and auto_now_add differ?</h3>
<p>auto_now_add only runs when creating (INSERT); auto_now runs on every save(). Both auto-fill timezone.now() and can't be edited through forms.</p>
<h3>Why doesn't update() change updated_at?</h3>
<p>update() runs SQL directly, skipping save(), so auto_now's pre_save never runs. Pass updated_at=timezone.now() into update() to change it explicitly.</p>
<h3>How do default=timezone.now and default=timezone.now() differ?</h3>
<p>Passing the function (no parentheses) makes Django call it again for each new record, so every record gets its own time. Calling the function upfront (with parentheses) makes all records share one moment computed when the module was imported — a classic bug.</p>
<h3>Should I use auto_now for a post's publish date?</h3>
<p>No, if the publish date is business data you need to edit by hand or import. Use default=timezone.now so it auto-fills and stays editable.</p>
<h3>Does bulk_update() refresh auto_now?</h3>
<p>No, same as update(). Pass updated_at explicitly or update each record with save().</p>
<p>💬 <strong>Have you tried this in your project?</strong> If you hit an error or have a neater way, leave a comment below — I read them all and will add the best ideas to the post.</p>
<h2>References</h2>
<ul>
<li>Django docs — DateField.auto_now / auto_now_add: <a href="https://docs.djangoproject.com/en/6.1/ref/models/fields/#django.db.models.DateField.auto_now">https://docs.djangoproject.com/en/6.1/ref/models/fields/#django.db.models.DateField.auto_now</a></li>
<li>Django docs — QuerySet.update(): <a href="https://docs.djangoproject.com/en/6.1/ref/models/querysets/#update">https://docs.djangoproject.com/en/6.1/ref/models/querysets/#update</a></li>
<li>Django docs — django.utils.timezone.now: <a href="https://docs.djangoproject.com/en/6.1/ref/utils/#django.utils.timezone.now">https://docs.djangoproject.com/en/6.1/ref/utils/#django.utils.timezone.now</a></li>
</ul>
<hr />
<p><em>Originally published at</em> <a href="https://linhfish.dev/en/2026/11/14/django-use-auto_now-and-auto_now_add-correctly-when-to-switch-to-defaulttimezone-now/"><em>linhfish.dev</em></a><em>.</em></p>
]]></content:encoded></item><item><title><![CDATA[Flask: Stop Requests the Standard Way with abort() — 404/403 with Custom Error Pages in 5 Minutes]]></title><description><![CDATA[When a user opens a post that does not exist, or visits the admin area without permission, the view must stop immediately and return the correct error code…]]></description><link>https://linhfishdev.hashnode.dev/flask-stop-requests-the-standard-way-with-abort-404-403-with-custom-error-pages-in-5-minutes</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/flask-stop-requests-the-standard-way-with-abort-404-403-with-custom-error-pages-in-5-minutes</guid><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Fri, 13 Nov 2026 15:18:54 GMT</pubDate><content:encoded><![CDATA[<p>When a user opens a post that does not exist, or visits the admin area without permission, the view must stop immediately and return the correct error code — not render an error template by hand and return it with status 200. Flask ships with <code>abort()</code>: it raises an HTTP exception, stops the view right there, and Flask returns the right status code with an error page. Combined with <code>errorhandler()</code>, you get a clean, standard error page for the whole application.</p>
<h2>The Problem</h2>
<p>The most common approach is to return an error string with a status code by hand: <code>return "Post not found", 404</code>. It works, but every view repeats the same pattern, and it is easy to forget the status code — leave out <code>, 404</code> and the error page returns 200, so Google indexes your "not found" page and clients or APIs cannot tell the request failed. Worse is rendering an error template and returning normally: the page looks right, but the status code is still 200.</p>
<pre><code class="language-python"># Weak approach: easy to forget the status code, every view repeats a pattern
@app.get("/posts/&lt;int:post_id&gt;")
def view_post(post_id):
    post = Post.query.get(post_id)
    if post is None:
        return "Post not found", 404  # forget ", 404" and it becomes 200
    return render_template("post.html", post=post)
</code></pre>
<h2>The Solution</h2>
<p><code>abort()</code> takes an HTTP error code and stops the view immediately — no code after it runs. To add a reason, pass <code>description</code> and Flask puts it on the default error page.</p>
<pre><code class="language-python">from flask import Flask, abort, render_template

app = Flask(__name__)

@app.get("/posts/&lt;int:post_id&gt;")
def view_post(post_id):
    post = Post.query.get(post_id)
    if post is None:
        abort(404, description=f"No post #{post_id}")
    if post.is_private and not current_user.is_admin:
        abort(403, description="This post is for administrators only")
    return render_template("post.html", post=post)
</code></pre>
<p>Flask's default error page is quite plain, so register <code>errorhandler()</code> once for the whole app to render your own nice template — the error object already carries <code>code</code> and <code>description</code> to display:</p>
<pre><code class="language-python">@app.errorhandler(404)
def page_not_found(error):
    return render_template("404.html", message=error.description), 404

@app.errorhandler(403)
def forbidden(error):
    return render_template("403.html", message=error.description), 403
</code></pre>
<p>One important note: <code>abort()</code> only makes sense inside a request context — in a view, in <code>before_request</code>, or in a helper called by a view. Outside a request — for example in a background job or the Flask shell — it still raises the HTTP exception, but there is no request for Flask to turn that exception into a response, so it is just an unhandled exception.</p>
<h2>Putting It Into Practice</h2>
<p>A quick rule for picking codes: resource not found means <code>abort(404)</code>, no permission means <code>abort(403)</code>, malformed input means <code>abort(400)</code>. Keep <code>description</code> short and user-facing ("No post #12") instead of leaking internal details. And register <code>errorhandler()</code> for 404 and 403 from the start of the project — once, and every <code>abort()</code> in the app gets a consistent error page.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/2026/09/30/flask-dung-application-factory-create_app-de-app-de-test-de-mo-rong-ngay-tu-dau/">Flask: Use the Application Factory (create_app) to Make the App Easy to Test and Extend from Day One</a></li>
<li><a href="https://linhfish.dev/2026/10/01/flask-to-chuc-route-gon-gang-bang-blueprint-khi-du-an-lon-dan/">Flask: Organize Routes Neatly with Blueprint as the Project Grows</a></li>
<li><a href="https://linhfish.dev/2026/10/03/flask-quan-ly-config-theo-moi-truong-dev-staging-prod-khong-con-hardcode-secret_key/">Flask: Manage Config per Environment (dev/staging/prod), No More Hardcoded SECRET_KEY</a></li>
</ul>
<h2>FAQ</h2>
<h3>How is abort() different from return ("…", 404)?</h3>
<p><code>abort()</code> raises an exception, so the view stops right there and the code after it never runs. A <code>return</code> with a tuple only sends the response after the view finishes — forget the status code and the response becomes 200, and you have to repeat this pattern in every view.</p>
<h3>Does the user see the description?</h3>
<p>Yes, if your error page uses it. Flask's default error page already shows the <code>description</code>; with a hand-written <code>errorhandler()</code>, read it from <code>error.description</code> and pass it to the template as in the example above.</p>
<h3>Can I use abort() outside a view?</h3>
<p>You should not. Outside a request context, <code>abort()</code> still raises the HTTP exception (for example <code>NotFound</code>), but there is no request for Flask to turn that exception into a response — in a background job or the Flask shell it is just an unhandled exception. Use <code>abort()</code> only in views, <code>before_request</code>, or helpers called by a view.</p>
<h3>How do I handle errors for an API?</h3>
<p>Register an <code>errorhandler()</code> that returns JSON instead of HTML, for example <code>{"error": error.description}</code> with <code>error.code</code>. The client gets the right error code and message instead of having to parse an HTML page.</p>
<p>Have you tried this approach in your own project? If you ran into an error or know a more concise way to do it, leave a comment below — I read every one and will add the best ideas to the post.</p>
<h2>Sources</h2>
<ul>
<li>Flask documentation, Application Errors: <a href="https://flask.palletsprojects.com/en/stable/errorhandling/">https://flask.palletsprojects.com/en/stable/errorhandling/</a></li>
<li>Flask documentation, API — abort(): <a href="https://flask.palletsprojects.com/en/stable/api/#flask.abort">https://flask.palletsprojects.com/en/stable/api/#flask.abort</a></li>
</ul>
<hr />
<p><em>Originally published at <a href="https://linhfish.dev/en/2026/11/13/flask-stop-requests-the-standard-way-with-abort-404-403-with-custom-error-pages-in-5-minutes/">linhfish.dev</a>.</em></p>
]]></content:encoded></item><item><title><![CDATA[Django: Write Create and Edit Views Twice as Fast with CreateView and UpdateView in 5 Minutes]]></title><description><![CDATA[When you need a form to create and edit a record, you don't have to rewrite the familiar GET/POST view pair. Django already ships two class-based views…]]></description><link>https://linhfishdev.hashnode.dev/django-write-create-and-edit-views-twice-as-fast-with-createview-and-updateview-in-5-minutes</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-write-create-and-edit-views-twice-as-fast-with-createview-and-updateview-in-5-minutes</guid><category><![CDATA[Django]]></category><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Thu, 12 Nov 2026 15:19:53 GMT</pubDate><content:encoded><![CDATA[<p>When you need a form to create and edit a record, you don't have to rewrite the familiar GET/POST view pair. Django already ships two class-based views built for exactly this job — <code>CreateView</code> and <code>UpdateView</code>: just declare the model, the list of fields and the template, and Django handles the rest — rendering the form, validating it, saving and redirecting. These two views fit best when your form maps directly onto a model through a <code>ModelForm</code>.</p>
<h2>The Problem</h2>
<p>In a blog app, a function-based view for creating a post is usually about 25 lines long: check <code>request.method</code>, build the form from <code>request.POST</code> or an empty form, call <code>is_valid()</code>, save and redirect. The view for editing a post is nearly identical, differing only in the line that fetches the object with <code>get_object_or_404</code> and passes <code>instance=post</code>. The two views are 80% the same, yet you still have to write and maintain them separately — and the more models you have, the more duplicated code piles up.</p>
<pre><code class="language-python"># views.py — function-based approach: long and repetitive
from django.shortcuts import get_object_or_404, redirect, render

from .forms import PostForm
from .models import Post

def post_create(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect("post-detail", pk=post.pk)
    else:
        form = PostForm()
    return render(request, "blog/post_form.html", {"form": form})

def post_update(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            return redirect("post-detail", pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, "blog/post_form.html", {"form": form})
</code></pre>
<h2>The Solution</h2>
<p>The two class-based views below do exactly the same job, and each one is only 4–5 lines of declarations. <code>CreateView</code> builds a <code>ModelForm</code> automatically from <code>model</code> and <code>fields</code>, renders the template following the <code>&lt;app&gt;/&lt;model&gt;_form.html</code> convention, saves the object when the form is valid and redirects to <code>success_url</code>. <code>UpdateView</code> works the same way, except that it fetches the object itself from the <code>pk</code> in the URL and pre-fills the form with that <code>instance</code>. Because <code>success_url</code> is evaluated when the module is imported — before the URLconf has finished loading — you must use <code>reverse_lazy()</code> instead of <code>reverse()</code>.</p>
<pre><code class="language-python"># views.py — same feature, declarations only
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView

from .models import Post

class PostCreateView(CreateView):
    model = Post
    fields = ["title", "content", "category"]
    template_name = "blog/post_form.html"
    success_url = reverse_lazy("post-list")

class PostUpdateView(UpdateView):
    model = Post
    fields = ["title", "content", "category"]
    template_name = "blog/post_form.html"
    success_url = reverse_lazy("post-list")
</code></pre>
<pre><code class="language-python"># urls.py
from django.urls import path

from .views import PostCreateView, PostUpdateView

urlpatterns = [
    path("posts/new/", PostCreateView.as_view(), name="post-create"),
    path("posts/&lt;int:pk&gt;/edit/", PostUpdateView.as_view(), name="post-update"),
]
</code></pre>
<p>Two common situations: you need to set the <code>author</code> before saving — override <code>form_valid()</code>, set <code>form.instance.author</code>, then call <code>super().form_valid(form)</code>. You need to stop other people editing posts that are not theirs — override <code>get_queryset()</code> so <code>UpdateView</code> only looks up posts belonging to the logged-in user; anyone else's post returns a 404 instead of opening the form.</p>
<pre><code class="language-python"># views.py — finished version: assign author + block editing other people's posts
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView

from .models import Post

class PostCreateView(CreateView):
    model = Post
    fields = ["title", "content", "category"]
    template_name = "blog/post_form.html"

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

    def get_success_url(self):
        return reverse_lazy("post-detail", kwargs={"pk": self.object.pk})

class PostUpdateView(UpdateView):
    model = Post
    fields = ["title", "content", "category"]
    template_name = "blog/post_form.html"
    success_url = reverse_lazy("post-list")

    def get_queryset(self):
        # Only look up your own posts: other people's posts return 404
        return Post.objects.filter(author=self.request.user)
</code></pre>
<h2>Apply It to Your Project</h2>
<p>Audit the create/edit views in your project: any GET/POST function pair repeated for each model can be replaced with <code>CreateView</code>/<code>UpdateView</code> following the pattern above. Start with the simplest model — a category, for example — run one create and one edit round of tests, then move on to models with their own logic, such as assigning an <code>author</code> or checking ownership.</p>
<h2>Related Posts</h2>
<ul>
<li><p><a href="https://linhfish.dev/en/2026/10/27/django-write-forms-twice-as-fast-with-modelform-stop-mapping-every-field-by-hand/">Django: Write Forms Twice as Fast with ModelForm — Stop Mapping Every Field by Hand</a></p>
</li>
<li><p><a href="https://linhfish.dev/en/2026/11/07/django-replace-repetitive-function-views-with-listview-and-detailview/">Django: Replace Repetitive Function Views with ListView and DetailView</a></p>
</li>
<li><p><a href="https://linhfish.dev/en/2026/11/10/django-stop-hardcoding-urls-in-views-and-templates-use-reverse-and-reverse_lazy-in-5-minutes/">Django: Stop Hardcoding URLs in Views and Templates — Use reverse() and reverse_lazy() in 5 Minutes</a></p>
</li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>Can CreateView and UpdateView share one template?</h3>
<p>Yes — both render the template following the <code>&lt;app&gt;/&lt;model&gt;_form.html</code> convention, so a single <code>post_form.html</code> template serves both creating and editing. To tell them apart, check <code>form.instance.pk</code> in the template: it is <code>None</code> for a new post and has a value when editing.</p>
<h3>When should I use <code>fields</code>, and when <code>form_class</code>?</h3>
<p>Use <code>fields</code> when the form is simple and you just need to quickly list the columns that may be edited. Use <code>form_class</code> with a dedicated <code>ModelForm</code> when you need custom widgets, <code>clean_</code> methods or English labels — in that case, do not declare <code>fields</code> anymore.</p>
<h3>Why must <code>success_url</code> use <code>reverse_lazy()</code> instead of <code>reverse()</code>?</h3>
<p>Because <code>success_url</code> is a class attribute, evaluated as soon as the module is imported — at that point the URLconf has not finished loading, so <code>reverse()</code> raises an error. <code>reverse_lazy()</code> defers the URL lookup until the redirect is actually needed. If you want a redirect that depends on the object just created, override <code>get_success_url()</code> as in the example above.</p>
<h3>Does UpdateView stop other people editing my posts on its own?</h3>
<p>No. By default <code>UpdateView</code> fetches the object by <code>pk</code> without checking ownership, so any logged-in user can edit any record. To block that, override <code>get_queryset()</code> to filter by <code>request.user</code> as in the example above, or combine it with <code>LoginRequiredMixin</code> and <code>UserPassesTestMixin</code>.</p>
<p>💬 <strong>Have you tried this in your own project?</strong> If you hit an error or have a neater way to do it, leave a comment below — I read them all and will fold the best ideas into the post.</p>
<h2>References</h2>
<ul>
<li><p>Django documentation — Generic editing views: <a href="https://docs.djangoproject.com/en/6.1/ref/class-based-views/generic-editing/">https://docs.djangoproject.com/en/6.1/ref/class-based-views/generic-editing/</a></p>
</li>
<li><p>Django documentation — ModelForm: <a href="https://docs.djangoproject.com/en/6.1/topics/forms/modelforms/">https://docs.djangoproject.com/en/6.1/topics/forms/modelforms/</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[FastAPI: Use Enums for Path Parameters — Endpoints That Only Accept Valid Values in 5 Minutes]]></title><description><![CDATA[Many APIs have path parameters that only accept a few fixed values — an order status, for example: pending, shipped, or delivered. Instead of accepting a…]]></description><link>https://linhfishdev.hashnode.dev/fastapi-use-enums-for-path-parameters-endpoints-that-only-accept-valid-values-in-5-minutes</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/fastapi-use-enums-for-path-parameters-endpoints-that-only-accept-valid-values-in-5-minutes</guid><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Wed, 11 Nov 2026 15:17:10 GMT</pubDate><content:encoded><![CDATA[<p>Many APIs have path parameters that only accept a few fixed values — an order status, for example: pending, shipped, or delivered. Instead of accepting a str and checking it by hand with if/else, declare the parameter as an Enum that inherits from str and Enum — FastAPI rejects unknown values with a 422 error on its own, generates docs with the list of choices, and the endpoint code no longer needs a branch for invalid values.</p>
<h2>The Problem</h2>
<p>Suppose you write an endpoint that lists orders by status. The familiar approach is to accept status as a str and branch through each value:</p>
<pre><code class="language-python">@app.get("/orders/{status}")
def list_orders(status: str):
    if status == "pending":
        return {"orders": ["#1001", "#1002"]}
    elif status == "shipped":
        return {"orders": ["#1003"]}
    elif status == "delivered":
        return {"orders": ["#1004", "#1005"]}
    return {"error": "Trạng thái không hợp lệ"}
</code></pre>
<p>This has three weaknesses. First, an unknown value still travels through the endpoint before being rejected, and the client gets a homemade error JSON instead of the framework's standard error. Second, the auto-generated docs only record status as a string, so API callers cannot know the valid values without reading the code. Third, every endpoint with a category-like parameter repeats the same if/else block, which gets harder to maintain over time.</p>
<h2>The Solution</h2>
<p>Declare an Enum that inherits from str and Enum, then use it as the parameter's type:</p>
<pre><code class="language-python">from enum import Enum

from fastapi import FastAPI

class OrderStatus(str, Enum):
    PENDING = "pending"
    SHIPPED = "shipped"
    DELIVERED = "delivered"

app = FastAPI()

ORDERS = {
    "pending": ["#1001", "#1002"],
    "shipped": ["#1003"],
    "delivered": ["#1004", "#1005"],
}

@app.get("/orders/{status}")
def list_orders(status: OrderStatus):
    return {"status": status.value, "orders": ORDERS[status.value]}
</code></pre>
<p>Inheriting from str keeps the Enum value a string when returned as JSON, while inheriting from Enum tells FastAPI the list of valid values. Inside the endpoint, status is guaranteed to be one of the three values, so you can use <code>status.value</code> directly without any extra check.</p>
<p>Verified with TestClient: GET /orders/pending returns 200 with the correct order list; GET /orders/cancelled returns 422 because the value is not in the Enum; GET /orders/PENDING also returns 422 because the comparison is case-sensitive. The docs at /docs show status as a dropdown, so callers just pick a value instead of guessing.</p>
<h2>Apply It to Your Project</h2>
<p>This pattern fits any parameter that only accepts a fixed set of values: order status, account type, interface language, priority level. The same Enum can be reused for query parameters and across many endpoints, and adding a new value means editing one place. If you want a clearer description in the docs, combine it with Path(description=…) for that parameter.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/en/2026/11/02/fastapi-validate-path-parameters-with-path-block-bad-ids-at-the-api-gateway-in-5-minutes/">FastAPI: Validate Path Parameters with Path() — Block Bad IDs at the API Gateway in 5 Minutes</a></li>
<li><a href="https://linhfish.dev/en/2026/11/06/fastapi-validate-query-strings-with-query-block-bad-parameters-at-the-api-gateway-in-5-minutes/">FastAPI: Validate Query Strings with Query() — Block Bad Parameters at the API Gateway in 5 Minutes</a></li>
<li><a href="https://linhfish.dev/en/2026/10/19/fastapi-bundle-a-dozen-query-params-into-one-pydantic-model-centralized-validation-in-5-minutes/">FastAPI: Bundle a Dozen Query Params into One Pydantic Model — Centralized Validation in 5 Minutes</a></li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>How is Enum different from Literal?</h3>
<p>Literal also lets FastAPI reject unknown values, but it is only a type hint attached in place. An Enum is a named type: it is reusable across many endpoints, can carry extra methods, and shows up clearly in the docs as its own schema.</p>
<h3>Can I use an integer Enum?</h3>
<p>Yes. Declare class Priority(int, Enum) with values 1, 2, 3 — FastAPI validates integers and the docs show the integer type instead of string.</p>
<h3>Are Enum values case-sensitive?</h3>
<p>Yes. "PENDING" does not match "pending", so FastAPI returns 422. If you want to accept both forms, normalize the input yourself before comparing, or define extra uppercase values in the Enum.</p>
<h3>Can Enum be used for query parameters?</h3>
<p>Yes, the declaration is identical: def search(status: OrderStatus = Query(…)). FastAPI validates the query parameter with the same Enum and shows the list of choices in the docs.</p>
<p>💬 <strong>Have you tried this in your own project?</strong> If you hit an error or know a shorter way to do it, leave a comment below — I read them all and will add the best ideas to the post.</p>
<h2>References</h2>
<ul>
<li>FastAPI — Path Parameters: <a href="https://fastapi.tiangolo.com/tutorial/path-params/">https://fastapi.tiangolo.com/tutorial/path-params/</a></li>
<li>Python docs — Enum: <a href="https://docs.python.org/3/library/enum.html">https://docs.python.org/3/library/enum.html</a></li>
</ul>
<hr />
<p><em>Originally published at <a href="https://linhfish.dev/en/2026/11/11/fastapi-use-enums-for-path-parameters-endpoints-that-only-accept-valid-values-in-5-minutes/">linhfish.dev</a>.</em></p>
]]></content:encoded></item><item><title><![CDATA[FastAPI: Write Standard CRUD with SQLAlchemy 2.0 — Session Dependency and select() in 5 Minutes]]></title><description><![CDATA[CRUD — the four create, read, update, and delete operations — is something almost every API needs. The standard way to do it in FastAPI is to put the…]]></description><link>https://linhfishdev.hashnode.dev/fastapi-write-standard-crud-with-sqlalchemy-2-0-session-dependency-and-select-in-5-minutes</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/fastapi-write-standard-crud-with-sqlalchemy-2-0-session-dependency-and-select-in-5-minutes</guid><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Tue, 10 Nov 2026 15:31:58 GMT</pubDate><content:encoded><![CDATA[<p>CRUD — the four create, read, update, and delete operations — is something almost every API needs. The standard way to do it in FastAPI is to put the database logic in CRUD functions built on SQLAlchemy 2.0 — <code>select()</code>, <code>db.get()</code>, <code>db.add()</code>, <code>db.delete()</code> — get the session from a <code>yield</code> dependency named <code>get_db</code>, and declare a clear <code>response_model</code> and <code>status_code</code> on each endpoint. This pattern works for any table, is easy to test with TestClient, and avoids both leaked sessions and sensitive data slipping into responses.</p>
<h2>The Problem</h2>
<p>Many tutorials still write CRUD by opening a session directly inside the endpoint, or by sharing one global session across all requests. A global session is unsafe when the server runs with multiple threads: two requests can interleave their commits, uncommitted data from one request can leak into another, and a session that is never closed slowly drains the connection pool. The old <code>Model.query</code> style has also been removed in SQLAlchemy 2.0 — old code will break when you upgrade.</p>
<pre><code class="language-python"># KHÔNG NÊN: session global dùng chung cho mọi request
SessionLocal = sessionmaker(bind=engine)
db = SessionLocal()  # một session duy nhất, không bao giờ đóng
@app.post("/items/")
def create_item(item: ItemCreate):
    db_item = Item(**item.model_dump())
    db.add(db_item)
    db.commit()
    return db_item
</code></pre>
<h2>The Solution</h2>
<p>Split the code into three clear layers. The model layer defines the table, the schema layer defines input and output data, and the CRUD layer holds all the SQL statements. Endpoints only call CRUD functions and return the right status code.</p>
<pre><code class="language-python"># models.py
from sqlalchemy import String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
    pass
class Item(Base):
    __tablename__ = "items"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(100))
    price: Mapped[float] = mapped_column(default=0.0)
# schemas.py
from pydantic import BaseModel, ConfigDict
class ItemCreate(BaseModel):
    name: str
    price: float = 0.0
class ItemRead(ItemCreate):
    id: int
    model_config = ConfigDict(from_attributes=True)
# crud.py
from sqlalchemy import select
from sqlalchemy.orm import Session
from models import Item
from schemas import ItemCreate
def get_items(db: Session, skip: int = 0, limit: int = 100):
    return db.execute(select(Item).offset(skip).limit(limit)).scalars().all()
def get_item(db: Session, item_id: int):
    return db.get(Item, item_id)  # tra None neu khong ton tai
def create_item(db: Session, item: ItemCreate):
    db_item = Item(**item.model_dump())
    db.add(db_item)
    db.commit()
    db.refresh(db_item)  # nap lai id vua sinh
    return db_item
def update_item(db: Session, item_id: int, item: ItemCreate):
    db_item = db.get(Item, item_id)
    if db_item is None:
        return None
    db_item.name = item.name
    db_item.price = item.price
    db.commit()
    db.refresh(db_item)
    return db_item
def delete_item(db: Session, item_id: int) -&gt; bool:
    db_item = db.get(Item, item_id)
    if db_item is None:
        return False
    db.delete(db_item)
    db.commit()
    return True
</code></pre>
<p>The session is provided through a <code>yield</code> dependency — FastAPI closes it automatically after the request finishes, whether or not the endpoint raised an error:</p>
<pre><code class="language-python"># database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
engine = create_engine("sqlite:///./app.db", connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(bind=engine)
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
</code></pre>
<p>Endpoints are then left with just calling CRUD functions, declaring the response clearly, and returning 404 when a record is not found:</p>
<pre><code class="language-python"># main.py
from fastapi import Depends, FastAPI, HTTPException, Response
from sqlalchemy.orm import Session
import crud
from database import Base, engine, get_db
from schemas import ItemCreate, ItemRead
Base.metadata.create_all(bind=engine)
app = FastAPI()
@app.get("/items/", response_model=list[ItemRead])
def list_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    return crud.get_items(db, skip=skip, limit=limit)
@app.get("/items/{item_id}", response_model=ItemRead)
def read_item(item_id: int, db: Session = Depends(get_db)):
    db_item = crud.get_item(db, item_id)
    if db_item is None:
        raise HTTPException(status_code=404, detail="Khong tim thay item")
    return db_item
@app.post("/items/", response_model=ItemRead, status_code=201)
def create_item(item: ItemCreate, db: Session = Depends(get_db)):
    return crud.create_item(db, item)
@app.put("/items/{item_id}", response_model=ItemRead)
def update_item(item_id: int, item: ItemCreate, db: Session = Depends(get_db)):
    db_item = crud.update_item(db, item_id, item)
    if db_item is None:
        raise HTTPException(status_code=404, detail="Khong tim thay item")
    return db_item
@app.delete("/items/{item_id}", status_code=204)
def delete_item(item_id: int, db: Session = Depends(get_db)):
    if not crud.delete_item(db, item_id):
        raise HTTPException(status_code=404, detail="Khong tim thay item")
    return Response(status_code=204)
</code></pre>
<p>Three details worth remembering: <code>select()</code> replaces the removed <code>Model.query</code>; <code>db.get()</code> returns <code>None</code>, so the endpoint itself decides to return 404; and <code>db.refresh()</code> after <code>commit()</code> fetches back the freshly generated <code>id</code>. A DELETE endpoint returns 204 with an empty body — use <code>Response(status_code=204)</code>, because returning <code>None</code> together with a <code>response_model</code> raises an error.</p>
<h2>Apply It to Your Project</h2>
<p>Apply this pattern to every resource in your API: each table gets its own CRUD module, and endpoints only call CRUD functions and declare a <code>response_model</code>. When you need pagination, add <code>skip</code>/<code>limit</code> to the <code>get_items</code> function instead of editing the endpoint; when you need tests, override the <code>get_db</code> dependency with an in-memory SQLite session — see the TestClient post already published.</p>
<h2>Frequently Asked Questions</h2>
<h3>Why not use one global session for every request?</h3>
<p>A SQLAlchemy session is not thread-safe: two requests sharing one session can interleave their commits, read each other’s uncommitted data, and a session that is never closed holds a connection until the pool runs dry. A <code>yield</code> dependency provides a separate session per request and closes it in <code>finally</code>, so none of those three problems occur.</p>
<h3>How is select() different from Model.query?</h3>
<p><code>Model.query</code> is the legacy API, removed entirely in SQLAlchemy 2.0. <code>select()</code> is the new standard API: it can express complex statements with joins, filters, and ordering in a single expression, and its results are fetched through the clearer <code>.scalars().all()</code> or <code>.scalar_one_or_none()</code>.</p>
<h3>When do I need db.refresh() after commit?</h3>
<p>After <code>commit()</code>, SQLAlchemy expires the object’s attributes. Call <code>db.refresh()</code> right after the commit to reload the freshly generated <code>id</code> and any database defaults before returning the object to the client.</p>
<h3>Do I need AsyncSession instead of the regular Session?</h3>
<p>The synchronous <code>Session</code> is enough for most APIs. Switch to <code>AsyncSession</code> only when you use an async driver such as asyncpg or aiosqlite, and never mix a regular <code>Session</code> with an <code>async def</code> endpoint, because it will block the event loop.</p>
<h2>References</h2>
<ul>
<li>FastAPI — SQL (Relational) Databases: <a href="https://fastapi.tiangolo.com/tutorial/sql-databases/">https://fastapi.tiangolo.com/tutorial/sql-databases/</a></li>
<li>SQLAlchemy 2.0 Documentation — ORM Querying Guide: <a href="https://docs.sqlalchemy.org/en/20/orm/queryguide/index.html">https://docs.sqlalchemy.org/en/20/orm/queryguide/index.html</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django Admin: Group Fields Neatly with fieldsets and Auto-Generate Slugs with prepopulated_fields in 5 Minutes]]></title><description><![CDATA[When a model has 10–15 fields, its edit page in Django admin becomes one endless column: title, slug, content, category, publish date, and status flags all…]]></description><link>https://linhfishdev.hashnode.dev/django-admin-group-fields-neatly-with-fieldsets-and-auto-generate-slugs-with-prepopulated-fields-in-5-minutes</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-admin-group-fields-neatly-with-fieldsets-and-auto-generate-slugs-with-prepopulated-fields-in-5-minutes</guid><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Mon, 09 Nov 2026 15:20:20 GMT</pubDate><content:encoded><![CDATA[<p>When a model has 10–15 fields, its edit page in Django admin becomes one endless column: title, slug, content, category, publish date, and status flags all jumbled together. The fix is <code>fieldsets</code> to group fields into titled, collapsible sections, combined with <code>prepopulated_fields</code> so the slug fills itself in from the title without typing. Both only need to be declared in <code>ModelAdmin</code>.</p>
<h2>The Problem</h2>
<p>By default, Django admin shows every field of a model in declaration order, stacked in a single column. For an article model with many fields, the edit page drags on forever: important fields like the title sit next to rarely used ones like the publish date. Worse, the <code>slug</code> field usually has to be typed by hand — a wrong format, a duplicate of an old slug, or a forgotten value all cause errors on save.</p>
<pre><code class="language-python"># admin.py — default: every field stacked in one long column
from django.contrib import admin
from .models import Article

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ("title", "category", "is_published")
</code></pre>
<h2>The Solution</h2>
<p>Declare <code>fieldsets</code> to split the form into groups, and <code>prepopulated_fields</code> so the slug fills in from the title as you type:</p>
<pre><code class="language-python"># admin.py
from django.contrib import admin
from .models import Article

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ("title", "category", "is_published")

    fieldsets = (
        (None, {
            "fields": ("title", "slug"),
        }),
        ("Content", {
            "fields": ("category", "content"),
        }),
        ("Publishing", {
            "classes": ("collapse",),
            "fields": ("is_published", "published_at"),
        }),
    )
    prepopulated_fields = {"slug": ("title",)}
</code></pre>
<p>Each tuple in <code>fieldsets</code> holds a group title and a describing dict: a first group with title <code>None</code> shows no separate heading. The “Publishing” group has <code>"classes": ("collapse",)</code>, so it starts collapsed — rarely used fields stop taking up space. <code>prepopulated_fields</code> takes a dict like <code>{"slug": ("title",)}</code>: as you type the title, the admin’s JavaScript generates the slug following the <code>slugify</code> standard, for example “My New Article” becomes <code>my-new-article</code>.</p>
<p>Three things to watch. First, never declare both <code>fields</code> and <code>fieldsets</code> in the same <code>ModelAdmin</code> — Django raises a configuration error. Second, <code>prepopulated_fields</code> only fills the slug in while it is empty, so a slug that is already saved is never overwritten when you edit the title later. Third, <code>slugify</code> strips accents by default, so generated slugs are always clean and URL-safe.</p>
<h2>Apply It to Your Project</h2>
<p>Open the <code>admin.py</code> of the app with the model that has the most fields, group the fields the way data is really entered: key information first, content in the middle, settings and status in a collapsed group at the end. Add <code>prepopulated_fields</code> to every model that has a <code>SlugField</code> next to a name or title field. After saving, reopen the edit page and check: the rarely used group must be collapsed, and the slug must generate correctly when creating a new record.</p>
<h2>Related Posts</h2>
<ul>
<li><p><a href="https://linhfish.dev/2026/10/09/django-admin-khoa-field-khong-cho-sua-bang-readonly_fields-trong-5-phut/">Django admin: khóa field không cho sửa bằng readonly_fields trong 5 phút</a></p>
</li>
<li><p><a href="https://linhfish.dev/2026/10/09/django-admin-tuy-bien-list_display-list_filter-search_fields-de-trang-quan-tri-nhanh-va-dung-de-hon/">Django admin: tùy biến list_display, list_filter, search_fields để trang quản trị nhanh và dùng dễ hơn</a></p>
</li>
<li><p><a href="https://linhfish.dev/en/2026/10/21/django-admin-inlines-edit-child-records-on-the-parent-detail-page-in-5-minutes/">Django Admin Inlines: Edit Child Records on the Parent Detail Page in 5 Minutes</a></p>
</li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>How are fieldsets different from fields?</h3>
<p><code>fields</code> only lists field order in a single column, while <code>fieldsets</code> splits the form into groups with their own titles and supports CSS classes like <code>collapse</code> or <code>wide</code>. Use <code>fields</code> when you only need to reorder; use <code>fieldsets</code> when you want grouping and collapsing.</p>
<h3>Does prepopulated_fields overwrite a saved slug?</h3>
<p>No. The admin JavaScript only fills the slug in while the slug field is empty, which is normally when creating a new record. When you edit an old record, the slug stays as it is even if you change the title — edit the slug by hand if you want it changed.</p>
<h3>What happens to accented characters in slugs?</h3>
<p><code>slugify</code> strips accents by default, so “Bài viết mới” becomes <code>bai-viet-moi</code>. If you want to keep accents in slugs, pass <code>allow_unicode=True</code> to <code>slugify</code> in <code>save()</code>, but accented URLs are error-prone when shared, so the default accent-stripping is the safer choice.</p>
<h3>Can fieldsets be used together with readonly_fields?</h3>
<p>Yes. A read-only field is declared normally inside <code>fieldsets</code>, and Django shows its value instead of an input box. This is handy when you want to group locked fields like the creation date into the collapsed group at the bottom of the page.</p>
<h2>References</h2>
<ul>
<li><p>Django documentation — ModelAdmin.fieldsets: <a href="https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets">https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets</a></p>
</li>
<li><p>Django documentation — ModelAdmin.prepopulated_fields: <a href="https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields">https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields</a></p>
</li>
<li><p>Haki Benita — Things You Must Know About Django Admin As Your App Gets Bigger: <a href="https://hakibenita.com/things-you-must-know-about-django-admin-as-your-app-gets-bigger">https://hakibenita.com/things-you-must-know-about-django-admin-as-your-app-gets-bigger</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[JavaScript: Run Multiple Promises at Once with Promise.allSettled — One Failure Won't Ruin the Batch]]></title><description><![CDATA[When you need to call several independent APIs at once — fetching user info, the post list, and statistics for a dashboard page, for example…]]></description><link>https://linhfishdev.hashnode.dev/javascript-run-multiple-promises-at-once-with-promise-allsettled-one-failure-won-t-ruin-the-batch</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/javascript-run-multiple-promises-at-once-with-promise-allsettled-one-failure-won-t-ruin-the-batch</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[promises]]></category><category><![CDATA[async]]></category><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Sun, 08 Nov 2026 15:16:15 GMT</pubDate><content:encoded><![CDATA[<p>When you need to call several independent APIs at once — fetching user info, the post list, and statistics for a dashboard page, for example — Promise.allSettled() is the right choice. It waits for every promise to finish, then returns each one's result, so one failing API doesn't throw away the data from the rest. Only use it when the tasks are independent of each other and you accept partial results; if every task must succeed, Promise.all() is still the better fit.</p>
<h2>The Problem</h2>
<p>Suppose a dashboard page calls three APIs at once with Promise.all():</p>
<pre><code class="language-javascript">const [user, posts, stats] = await Promise.all([
  fetch("/api/user").then((r) =&gt; r.json()),
  fetch("/api/posts").then((r) =&gt; r.json()),
  fetch("/api/stats").then((r) =&gt; r.json()),
]);
</code></pre>
<p>One 500 error from the stats API rejects the whole Promise.all() immediately. The other two APIs may have returned correct data, but it gets thrown away, the dashboard goes blank, and the user has to reload everything. The failure is even more annoying because the APIs have nothing to do with each other: the user data and the posts could easily show first, with only the stats block reporting an error.</p>
<h2>The Solution</h2>
<p>Promise.allSettled() waits for all promises to settle — fulfilled or rejected — then returns an array of results in the same order as the input. Each element has a status of "fulfilled" with a value, or "rejected" with a reason, so you filter out the successful ones to display and handle the failures separately:</p>
<pre><code class="language-javascript">const results = await Promise.allSettled([
  fetch("/api/user").then((r) =&gt; r.json()),
  fetch("/api/posts").then((r) =&gt; r.json()),
  fetch("/api/stats").then((r) =&gt; r.json()),
]);

const [user, posts, stats] = results.map((r) =&gt;
  r.status === "fulfilled" ? r.value : null
);

if (stats === null) {
  console.warn("Không tải được thống kê:", results[2].reason);
}
</code></pre>
<p>With this approach, the dashboard still shows the user info and the posts, and only the stats block shows an error message. You can also collect the errors for logging, or retry only the failed APIs instead of calling all three again.</p>
<h2>Apply It to Your Project</h2>
<p>Use Promise.allSettled() anywhere you call several independent tasks and want to keep partial results: a dashboard calling many APIs, an import job processing a batch of files, a status screen checking many services at once, or a feature sending email to many recipients where one bad address must not block the rest. The rule to remember: independent tasks → allSettled, tasks that must all succeed → Promise.all.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/2026/10/18/javascript-fetch-khong-tu-timeout-huy-request-treo-bang-abortcontroller-trong-5-phut/">JavaScript: fetch() không tự timeout — hủy request treo bằng AbortController trong 5 phút</a></li>
<li><a href="https://linhfish.dev/2026/10/02/temporal-ve-dich-javascript-cuoi-cung-cung-co-api-xu-ly-ngay-gio-chuan-dung-duoc-ngay-tren-chrome-firefox-va-node-26/">Temporal "về đích": JavaScript cuối cùng cũng có API xử lý ngày giờ chuẩn — dùng được ngay trên Chrome, Firefox và Node 26</a></li>
<li><a href="https://linhfish.dev/2026/09/28/node-js-26-10-0-ra-mat-crypto-parsepkcs12-fs-openasblobsync-util-throttle-debounce-va-cai-tien-sqlite/">Node.js 26.10.0 ra mắt: crypto.parsePKCS12(), fs.openAsBlobSync(), util.throttle()/debounce() và cải tiến SQLite</a></li>
</ul>
<h2>FAQ</h2>
<h3>How is Promise.allSettled() different from Promise.all()?</h3>
<p>Promise.all() stops as soon as one promise rejects and discards all results, while Promise.allSettled() waits for everything to finish and returns each task's status, so you always get the data from the tasks that succeeded.</p>
<h3>Do the results keep the same order as the input promises?</h3>
<p>Yes. The result array is always ordered exactly like the promise array you passed in, no matter which promise finished first or last, so you can assign results by index as in the example above.</p>
<h3>When should I still use Promise.all() instead of Promise.allSettled()?</h3>
<p>When the steps depend on each other or all of them must succeed — creating an order before deducting stock, for example. Then one failing step has to stop the whole flow, and Promise.all()'s fail-fast behavior is the right one.</p>
<h3>Which browsers and Node.js versions support Promise.allSettled()?</h3>
<p>It's an ES2020 feature, available in Chrome 76+, Firefox 71+, Safari 13+, and Node.js 12.9 and later. For very old browsers, add a polyfill or use a library like core-js.</p>
<p>💬 <strong>Have you tried this in your own project?</strong> If you hit an error or know a shorter way, leave a comment below — I read them all and will add the good ideas to the post.</p>
<h2>References</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled">MDN Web Docs — Promise.allSettled()</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">MDN Web Docs — Using promises</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Django: Send Email with send_mail() in 5 Minutes — Configure SMTP the Right Way]]></title><description><![CDATA[Django already ships with send_mail() — configure SMTP once in settings.py and you can send email with a single call. Use it for welcome emails after…]]></description><link>https://linhfishdev.hashnode.dev/django-send-email-with-send-mail-in-5-minutes-configure-smtp-the-right-way</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/django-send-email-with-send-mail-in-5-minutes-configure-smtp-the-right-way</guid><category><![CDATA[Django]]></category><category><![CDATA[Python]]></category><category><![CDATA[email]]></category><category><![CDATA[smtp]]></category><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Sat, 07 Nov 2026 15:17:53 GMT</pubDate><content:encoded><![CDATA[<p>Django already ships with <code>send_mail()</code> — configure SMTP once in <code>settings.py</code> and you can send email with a single call. Use it for welcome emails after registration, order confirmations, password resets, or system notifications. No extra library to install, and no hand-rolled SMTP code with <code>smtplib</code>.</p>
<h2>The Problem</h2>
<p>Many new projects call <code>send_mail()</code> right away without configuring anything, assuming Django “just knows” where to send mail. In reality Django defaults to an SMTP backend pointing at <code>localhost:25</code> — most dev machines have no mail server, so the request hangs and then fails with <code>ConnectionRefusedError</code>, and users who just registered wait forever for a welcome email that never arrives. Another common mistake is hardcoding the email password straight into <code>settings.py</code> and accidentally committing it to Git — the mailbox gets exposed and abused to send spam.</p>
<pre><code class="language-python">from django.core.mail import send_mail

send_mail(
    "Chao mung ban den voi MyShop",
    "Cam on ban da dang ky tai khoan.",
    "noreply@example.com",
    ["khach@example.com"],
    fail_silently=False,
)
# ConnectionRefusedError: [Errno 111] Connection refused (localhost:25)
</code></pre>
<h2>The Solution</h2>
<p>The right approach is to move the configuration into environment variables and let Django handle the SMTP part. In <code>settings.py</code>, declare the SMTP backend with its host, port, and credentials — the password comes from an environment variable, never written directly into the code. For local development, switch to the console backend so mail is printed to the terminal instead of actually sent, and you never risk emailing real users by accident.</p>
<pre><code class="language-python">import os

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ["EMAIL_HOST_USER"]
EMAIL_HOST_PASSWORD = os.environ["EMAIL_HOST_PASSWORD"]
DEFAULT_FROM_EMAIL = "MyShop &lt;noreply@example.com&gt;"

# Dev local: in mail ra terminal thay vi gui that
# EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
</code></pre>
<p>After that, anywhere in your code — a view, a signal, or a management command — one <code>send_mail()</code> call with four arguments is enough: subject, plain-text body, sender address, and the list of recipients. The function returns the number of emails sent successfully, usually <code>1</code>; if SMTP fails and <code>fail_silently=False</code> (the default), Django raises an exception so you can handle it instead of silently swallowing the error. For a nicer HTML version, pass <code>html_message</code> — Django automatically attaches the plain-text body as a fallback for old mail clients.</p>
<pre><code class="language-python">from django.core.mail import send_mail

sent = send_mail(
    "Don hang #123 da duoc xac nhan",
    "Don hang #123 cua ban da duoc xac nhan.",
    "noreply@example.com",
    ["khach@example.com"],
    html_message="&lt;h1&gt;Don hang #123&lt;/h1&gt;&lt;p&gt;Cam on ban da mua hang.&lt;/p&gt;",
)
print(sent)  # 1
</code></pre>
<h2>Apply It to Your Project</h2>
<p>Wire <code>send_mail()</code> into the right touchpoints: a welcome email after user registration (combined with the <code>post_save</code> signal), a confirmation email after an order is created, a password-reset email. Sending over SMTP usually takes 1–3 seconds, so don’t call it directly in a view if you don’t want users waiting — push it to a background task or run it after the transaction commits successfully, and always log failures so you can resend later.</p>
<h2>Related Posts</h2>
<ul>
<li><p><a href="https://linhfish.dev/en/2026/10/13/flask-sending-email-with-flask-mail-dont-let-smtp-block-your-requests-5-minutes/">Flask: Sending Email with Flask-Mail — Don’t Let SMTP Block Your Requests (5 Minutes)</a></p>
</li>
<li><p><a href="https://linhfish.dev/2026/09/30/fastapi-xu-ly-viec-nang-o-background-voi-backgroundtasks-client-khong-phai-cho/">FastAPI: Handle Heavy Work in the Background with BackgroundTasks — Clients Don’t Have to Wait</a></p>
</li>
<li><p><a href="https://linhfish.dev/en/2026/10/15/django-run-tasks-after-successful-commit-with-transaction-on_commit-in-5-minutes/">Django: Run Tasks After Successful Commit with transaction.on_commit in 5 Minutes</a></p>
</li>
</ul>
<h2>Frequently Asked Questions</h2>
<h3>Can I send email with Gmail?</h3>
<p>Yes. Use host <code>smtp.gmail.com</code>, port 587, enable TLS, and use an App Password instead of your main password — Gmail has blocked sign-in with regular passwords since 2022. Remember to turn on two-step verification before creating an App Password.</p>
<h3>How do I test sending email without actually sending it?</h3>
<p>Set <code>EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'</code> in your dev settings — mail will be printed to the terminal instead of leaving your machine. To assert on mail in tests, use the locmem backend and inspect <code>django.core.mail.outbox</code>.</p>
<h3>Can send_mail() send to many people at once?</h3>
<p>Yes — pass a list of addresses to <code>recipient_list</code> and they all receive the same email. If you need to send a bulk batch where each person gets different content, use <code>send_mass_mail()</code> to open a single SMTP connection instead of reopening one per email.</p>
<h3>What if my emails keep landing in spam?</h3>
<p>Check three things: the sender address must belong to a domain you own, that domain must have SPF/DKIM/DMARC configured, and the content should avoid spam-like wording. Also, sending from a low-reputation SMTP account is more likely to get filtered — consider a dedicated email-sending service when your volume grows.</p>
<p>💬 <strong>Have you tried this in your own project</strong>? If you hit an error or know a shorter way to do it, leave a comment below — I read every one and will fold the best ideas into the post.</p>
<h2>References</h2>
<ul>
<li><p>Django documentation — Sending email: <a href="https://docs.djangoproject.com/en/stable/topics/email/">https://docs.djangoproject.com/en/stable/topics/email/</a></p>
</li>
<li><p>Django documentation — django.core.mail: <a href="https://docs.djangoproject.com/en/stable/topics/email/#send-mail">https://docs.djangoproject.com/en/stable/topics/email/#send-mail</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Python: Call Multiple APIs in Parallel with asyncio.gather and Semaphore in 10 Minutes]]></title><description><![CDATA[When you need to call dozens of APIs — syncing data, crawling prices, firing webhooks — a sequential for loop adds up the waiting time of every request: 20…]]></description><link>https://linhfishdev.hashnode.dev/python-call-multiple-apis-in-parallel-with-asyncio-gather-and-semaphore-in-10-minutes</link><guid isPermaLink="true">https://linhfishdev.hashnode.dev/python-call-multiple-apis-in-parallel-with-asyncio-gather-and-semaphore-in-10-minutes</guid><dc:creator><![CDATA[Linh Fish Dev]]></dc:creator><pubDate>Sat, 07 Nov 2026 14:21:30 GMT</pubDate><content:encoded><![CDATA[<h1>Python: Call Multiple APIs in Parallel with asyncio.gather and Semaphore in 10 Minutes</h1>
<p>When you need to call dozens of APIs — syncing data, crawling prices, firing webhooks — a sequential <code>for</code> loop adds up the waiting time of every request: 20 requests × 1 second = 20 seconds. <code>asyncio.gather</code> runs many coroutines concurrently inside a single event loop, while <code>asyncio.Semaphore</code> limits how many requests fly at the same time so you don't overwhelm the partner API. This technique applies to every I/O-bound task (HTTP calls, file reads, database queries) and only needs the <code>httpx</code> library.</p>
<h2>The Problem: a Sequential Loop Adds Up All the Waiting</h2>
<p>Say you need to fetch info for 20 users from a partner API, each request taking about 1 second. The most "naive" code is a loop with the synchronous <code>httpx.Client</code>:</p>
<pre><code class="language-python">import time
import httpx

URLS = [f"https://api.example.com/users/{i}" for i in range(20)]

def fetch_all():
    results = []
    with httpx.Client(timeout=10) as client:
        for url in URLS:
            results.append(client.get(url).json())
    return results

t0 = time.perf_counter()
fetch_all()
print(f"finished in {time.perf_counter() - t0:.1f} seconds")
</code></pre>
<p>This code runs correctly, but the total time is ≈ 20 seconds because each request waits for the previous one to finish. The CPU sits idle almost the whole time — the bottleneck is I/O waiting, not processing power. With 200 records, you'd wait over 3 minutes.</p>
<h2>The Solution: asyncio.gather + Semaphore</h2>
<p><code>asyncio</code> lets many coroutines "suspend" at I/O waiting points and hand the event loop to another coroutine. <code>asyncio.gather(*coros)</code> groups them and runs them concurrently, returning results in the same order you passed in. But don't fire 200 requests at once — the partner API may rate-limit you or block your IP. <code>asyncio.Semaphore(n)</code> is a "gate" that lets only <code>n</code> coroutines through at the same time:</p>
<pre><code class="language-python">import asyncio
import httpx

async def fetch_one(client, url, sem):
    async with sem:  # grab a ticket before calling HTTP
        resp = await client.get(url, timeout=10)
        resp.raise_for_status()
        return resp.json()

async def fetch_all_async(urls):
    sem = asyncio.Semaphore(5)  # at most 5 requests at once
    async with httpx.AsyncClient() as client:
        return await asyncio.gather(
            *(fetch_one(client, u, sem) for u in urls),
            return_exceptions=True,
        )

results = asyncio.run(fetch_all_async(URLS))
</code></pre>
<p>How it flows: each <code>fetch_one</code> grabs a "ticket" from the semaphore before calling HTTP; at most 5 requests fly together, and when one finishes another takes its place. With 20 requests × 1 second and <code>Semaphore(5)</code>, total time drops to ≈ 4 seconds instead of 20. Measured on a local server (20 requests × 0.3 seconds): 6.19 seconds → 1.41 seconds.</p>
<h2>Collect Errors Neatly with return_exceptions=True</h2>
<p>By default, one failing coroutine makes <code>gather</code> raise the exception immediately, and you lose the results of the requests that already finished. Pass <code>return_exceptions=True</code> and the error comes back as a normal item in the result list — filter it afterwards:</p>
<pre><code class="language-python">ok = [r for r in results if not isinstance(r, Exception)]
failed = [r for r in results if isinstance(r, Exception)]
print(f"success: {len(ok)}/{len(results)}")
for e in failed:
    print("failed request:", repr(e))
</code></pre>
<p>This pattern shines in data-sync jobs: 98 of 100 records still get processed, and the 2 failures go to the log for a later retry — instead of the whole batch dying because of one bad request.</p>
<h2>Apply It to Your Project</h2>
<p>Start with <code>Semaphore(5)</code>, then raise it gradually while watching response times and 429 codes from the partner API. Always set a <code>timeout</code> on every request — one request hanging forever holds its semaphore "ticket" forever and jams the whole batch. If your project already uses FastAPI, your <code>async def</code> endpoints can <code>await</code> <code>fetch_all_async()</code> directly without <code>asyncio.run()</code>.</p>
<h2>Related Posts</h2>
<ul>
<li><a href="https://linhfish.dev/en/2026/10/15/python-stop-using-print-for-production-debugging-log-properly-with-logging-in-5-minutes/">Python: Stop Using print() for Production Debugging — Log Properly with logging in 5 Minutes</a></li>
<li><a href="https://linhfish.dev/en/2026/10/17/python-speed-up-repeated-calculations-with-lru_cache-and-cache-in-5-minutes/">Python: Speed Up Repeated Calculations with @lru_cache and @cache in 5 Minutes</a></li>
</ul>
<h2>FAQ</h2>
<h3>What should I set Semaphore(n) to?</h3>
<p>There's no magic number: start at 5–10, increase gradually, and watch for 429 codes or timeouts from the partner API. Many public APIs limit you to 5–10 requests per second.</p>
<h3>Does gather keep the result order?</h3>
<p>Yes. Results come back in the order you passed the coroutines in, not the order they finish. That's the difference from <code>asyncio.as_completed()</code>.</p>
<h3>Can one bad request kill the whole batch?</h3>
<p>By default, yes — <code>gather</code> raises the first exception it meets. Use <code>return_exceptions=True</code> to collect errors into the list and handle them later, as shown above.</p>
<h3>When should I NOT use asyncio?</h3>
<p>For CPU-bound work (image processing, file compression, heavy math): the GIL prevents coroutines from truly running in parallel. Use <code>multiprocessing</code> or <code>concurrent.futures.ProcessPoolExecutor</code> for those.</p>
<p>💬 <strong>Have you tried this in your project?</strong> If you hit an error or know a cleaner way, leave a comment below — I read them all and will add the best ideas to this post.</p>
<h2>References</h2>
<ul>
<li>Python docs — asyncio: <a href="https://docs.python.org/3/library/asyncio.html">https://docs.python.org/3/library/asyncio.html</a></li>
<li>httpx docs — Async API: <a href="https://www.python-httpx.org/async/">https://www.python-httpx.org/async/</a></li>
</ul>
<hr />
<p><em>Originally published at <a href="https://linhfish.dev/en/2026/10/23/python-call-multiple-apis-in-parallel-with-asyncio-gather-and-semaphore-in-10-minutes/">linhfish.dev</a>.</em></p>
]]></content:encoded></item></channel></rss>