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

Version of dumpdata that includes soft deleted models #198

Open
danizen opened this issue Jan 24, 2022 · 4 comments
Open

Version of dumpdata that includes soft deleted models #198

danizen opened this issue Jan 24, 2022 · 4 comments

Comments

@danizen
Copy link

danizen commented Jan 24, 2022

We had a situation arise with an application written in 2017 using a home-grown ModelManager which works similarly to the SafeDeleteManager. Because we are migrating from one database to another, and there is not too much data, we attempted to use the management commands dumpdata and loaddata to copy the data to the new database instance.

This caused referential integrity errors because dumpdata did not include the deleted models. My suggestion is that safedelete includes its own dumpdata command which avoids the problem:

from types import MethodType
from django.apps import apps
from django.db import models
from django.core.management.commands.dumpdata import Command as BaseCommand

# not quite using django-safedelete, but other apps here do
from lrmaint.models import SoftDeleteManager


class Command(BaseCommand):

    def handle(self, *app_labels, **options):
        print('This is a wrapper around Django default dumpdata')

        # patch each softdeletemanager to avoid overload
        for model in apps.get_app_config('lrmaint').get_models():
            if isinstance(model.objects, SoftDeleteManager):
                model.objects.get_queryset = MethodType(models.Manager.get_queryset, model.objects)

        # defer to dumpdata
        super().handle(*app_labels, **options)
@danizen
Copy link
Author

danizen commented Jan 24, 2022

Another way to mitigate this would be to mention it in the documentation as a warning.

@danizen
Copy link
Author

danizen commented Jan 24, 2022

The actual result was that dump worked, but load failed with a referential integrity error.

@Gagaro
Copy link
Member

Gagaro commented Jan 28, 2022

Thanks for the report 👍 . Feel free to open a PR implementing this.

@yourcelf
Copy link

yourcelf commented Sep 1, 2022

This can be done using the built-in dumpdata by passing the -a/--all flag:

./manage.py dumpdata --all myapp

That causes dumpdata to use the base manager rather than the default manager, and results in safe-deleted objects being included in the dump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants