Code

Opened 6 years ago

Closed 5 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: master
Severity: Keywords: dumpdata
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 6 years ago.
Patches the dumpdata command
6155-2.diff (1.1 KB) - added by empty 6 years ago.
Added documentation patch

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by empty

Patches the dumpdata command

comment:1 Changed 6 years ago by empty

  • Needs documentation unset
  • Needs tests set
  • Owner changed from nobody to empty
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 6 years ago by Simon G <dev@…>

  • Triage Stage changed from Unreviewed to Ready for checkin

comment:3 Changed 6 years ago by empty

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.

Changed 6 years ago by empty

Added documentation patch

comment:4 Changed 6 years ago by empty

  • Has patch set
  • Needs tests unset
  • Owner changed from empty to nobody
  • Status changed from assigned to new

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 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(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 Changed 5 years ago by mk

  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Ready for checkin to Unreviewed

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 Changed 5 years ago by Alex

  • Resolution set to fixed
  • Status changed from reopened to closed

Please open a new ticket.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.