Opened 16 years ago

Closed 15 years ago

#6155 closed (fixed)

Dumpdata Fails If There Is No Objects Attribute When Using a Custom Manager

Reported by: empty Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords: dumpdata
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Dumpdata fails if there is no objects default manager when Including your own custom manager. For example if we have a model that looks like:

class CategoryManager(models.Manager):
    def find_django(self):
        return self.filter(name='Django').order_by('name',)

class Category(models.Model):
    name = models.CharField(max_length=50)
    user = models.ManyToManyField(User)
    
    active_objects = CategoryManager()

Since there is no objects attribute on the model when the following dumpdata code runs it will fail with an attribute error:

        objects = []
        for app in app_list:
            for model in get_models(app):
                objects.extend(model.objects.all())

See http://groups.google.com/group/django-users/browse_thread/thread/7a58d619aea92d9c for more information.

The fix is to modify the block so it looks like this:

        objects = []
        for app in app_list:
            for model in get_models(app):
                objects.extend(model._default_manager.all())

Attachments (2)

6155-1.diff (592 bytes ) - added by empty 16 years ago.
Patches the dumpdata command
6155-2.diff (1.1 KB ) - added by empty 16 years ago.
Added documentation patch

Download all attachments as: .zip

Change History (9)

by empty, 16 years ago

Attachment: 6155-1.diff added

Patches the dumpdata command

comment:1 by empty, 16 years ago

Needs tests: set
Owner: changed from nobody to empty
Status: newassigned

comment:2 by Simon G <dev@…>, 16 years ago

Triage Stage: UnreviewedReady for checkin

comment:3 by empty, 16 years ago

One issue to be aware of with the above solution is that it uses the same logic for the Admin piece. This means if the model looks something like:

class Category(models.Model):
    name = models.CharField(max_length=50)
    user = models.ManyToManyField(User)
    
    active_objects = CategoryManager()
    objects = models.Manager()

The dumpdata will dump from the default manager (in this case CategoryManager) instead of the models.Manager(). If the get_query_set() method is modified to filter records in a special way and the custom manager appears first in the model class then dumpdata will only provide the result of that modified queryset.

If this is okay the above solution should work fine. If it's not then we'll need to check if the objects attribute is available and use that otherwise use _default_manager.

by empty, 16 years ago

Attachment: 6155-2.diff added

Added documentation patch

comment:4 by empty, 16 years ago

Has patch: set
Needs tests: unset
Owner: changed from empty to nobody
Status: assignednew

I added documentation to the django-admin.py docs to explain that the default manager is used. After looking through tests I really couldn't find anything that was not already covered. The tests for covering the default manager already exist in the custom_managers tests. If there's something that you would like me to add, please let me know and I'll be happy to do it.

comment:5 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [6932]) Fixed #6155 -- Fixed dumpdata to work with the default model manager (necessary
for the rare cases when the 'objects' manager might not even exist). Based on
a patch from Michael Trier.

comment:6 by Matthias Kestenholz, 15 years ago

Resolution: fixed
Status: closedreopened
Triage Stage: Ready for checkinUnreviewed

The fix from [6932] should be reapplied (it was reverted -- probably inadvertently -- in [9921])

Maybe this is not the best way for dumpdata to work though (See ticket #7566)

comment:7 by Alex Gaynor, 15 years ago

Resolution: fixed
Status: reopenedclosed

Please open a new ticket.

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