About Archiving Outdated Model Instances in Another Database
When using multi-database setup, you can archive the models from the main database by saving them on the secondary database and deleting them from the main one:
# Get an entry from the default database
post = Post.objects.get(slug="hello-world")
# Copy categories to the archive database
categories_in_archive_db = []
for category in post.categories.all():
category.save(using="archive")
categories_in_archive_db.append(category)
# Save the entry to the archive database
post.save(using="archive")
post.categories.set(categories_in_archive_db)
# Delete the entry from the default database
post.refresh_from_db(using="default")
post.delete()
Tips and Tricks Programming Development Databases Django 5.2 Django 4.2 Django 3.2 PostgreSQL MySQL SQLite
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.