Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use specific database for write operations in multi-DB setup #124

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions treenode/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models, transaction
from django.db import models, router, transaction
from django.utils.encoding import force_str
from django.utils.html import conditional_escape
from django.utils.text import slugify
Expand Down Expand Up @@ -147,7 +147,7 @@ def delete(self, using=None, keep_parents=False, cascade=True):
@classmethod
def delete_tree(cls):
with no_signals():
with transaction.atomic():
with transaction.atomic(using=router.db_for_write(TreeNodeModel)):
cls.objects.all().delete()
clear_refs(cls)
clear_cache(cls)
Expand Down Expand Up @@ -424,7 +424,7 @@ def update_tree(cls):
# update db
objs_data = cls.__get_nodes_data()

with transaction.atomic():
with transaction.atomic(using=router.db_for_write(TreeNodeModel)):
obj_manager = cls.objects
for obj_pk, obj_data in objs_data.items():
obj_manager.filter(pk=obj_pk).update(**obj_data)
Expand Down