About Relations in Multi-database Project

When using a multi-database project and creating entries, you must ensure that all foreign key and many-to-many relations for an entry are from the same database. Otherwise, Django will raise exceptions.

from posts.models import Post, Category

post = Post(title="Hello, World!", content="...")
post.save(using="archive")
categories = Category.objects.using("archive").filter(
    slug__in=["introduction", "getting-started"],
)
post.categories.set(categories)

Tips and Tricks Programming Development Databases Django 5.2 Django 4.2 Django 3.2 PostgreSQL MySQL SQLite