Opened 6 weeks ago

Closed 6 weeks ago

#35898 closed New feature (wontfix)

Cannot use base managers of related models in dumpdata command

Reported by: Lorenzo Martini Owned by:
Component: Core (Serialization) Version: 5.1
Severity: Normal Keywords: dumpdata chunk_size iterator prefetch_related user base_manager default_manager
Cc: Lorenzo Martini Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Lorenzo Martini)

Hello Django Team,

I'm encountering an issue with the django-admin dumpdata command when trying to serialize a model with a ManyToMany relationship to a custom User model. This custom User model inherits from another User model defined in a different app.

It appears that Django is using the default manager of the custom User model, rather than its base manager. This custom manager includes prefetch_related calls in its get_queryset function, which I suspect is causing the issue.

I've attached a Dockerized Django app that replicates this behavior. You can also find it on GitHub: https://github.com/Martini991/django-dumpdata-bug.

For reference, I’ve reviewed two related tickets, but they seem to address different issues:
https://code.djangoproject.com/ticket/35238
https://code.djangoproject.com/ticket/35159

Thank you for your assistance!

Attachments (1)

django-dumpdata-bug.zip (13.9 KB ) - added by Lorenzo Martini 6 weeks ago.

Download all attachments as: .zip

Change History (3)

by Lorenzo Martini, 6 weeks ago

Attachment: django-dumpdata-bug.zip added

comment:1 by Lorenzo Martini, 6 weeks ago

Description: modified (diff)

comment:2 by Sarah Boyce, 6 weeks ago

Resolution: wontfix
Status: newclosed
Summary: dumpdata Command Fails on Model with Custom User ManagerCannot use base managers of related models in dumpdata command
Type: BugNew feature

Hi Lorenzo

I don't think this is a bug but a new feature potentially. I think the issue can be roughly demonstrated like:

class CustomManager(models.Manager):
    use_in_migrations = False

    def get_queryset(self):
        # This will fail!
        return super().get_queryset().filter(author="Roald Dahl")

class CustomManagerModel(models.Model):
    objects = CustomManager()


class NewModel(models.Model):
    customs = models.ManyToManyField('CustomManagerModel', related_name='new', blank=True)

Then you run dumpdata for NewModel with the --all flag (https://docs.djangoproject.com/en/5.1/ref/django-admin/#cmdoption-dumpdata-all) but this means you use the base manager for that model, not all related models

I think this use case is quite niche but feel free to propose it on the Forum and see if others agree this should be supported

Note: See TracTickets for help on using tickets.
Back to Top