Opened 17 years ago
Closed 16 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)
Change History (9)
by , 17 years ago
Attachment: | 6155-1.diff added |
---|
comment:1 by , 17 years ago
Needs tests: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
comment:3 by , 17 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.
comment:4 by , 17 years ago
Has patch: | set |
---|---|
Needs tests: | unset |
Owner: | changed from | to
Status: | assigned → 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 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:6 by , 16 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
Triage Stage: | Ready for checkin → Unreviewed |
Patches the dumpdata command