Code

Opened 5 years ago

Closed 5 years ago

#9750 closed (wontfix)

Manager Documentation and Admin interface

Reported by: mm Owned by: nobody
Component: Documentation Version: 1.0
Severity: Keywords:
Cc: martin@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In http://docs.djangoproject.com/en/dev/topics/db/managers/ it says:

If you use custom Manager objects, take note that the first Manager Django encounters
(in the order in which they're defined in the model) has a special status. Django
interprets this first Manager defined in a class as the "default" Manager, and several
parts of Django (though not the admin application) will use that Manager exclusively for
that model. As a result, it's often a good idea to be careful in your choice of default
manager, in order to avoid a situation where overriding of get_query_set() results in an
inability to retrieve objects you'd like to work with.

I found this to be wrong. The admin interface will only list the objects that are in the queryset that the default manager returns

This will only return objects with a status of finished=True in the admin, it's not even possible to access them directly by URL (default is to create objects that won't show)

Note: code written from scratch but you should get the idea, mind the order of appearance of the managers...

from django.db import models
from django.contrib import admin

class FinishedManager(models.Manager):
    def get_query_set(self):
        return super(FinishedManager, self).get_query_set().filter(finished=True)

class Todo(models.Model):
    title = models.CharField(max_length=250)
    finished = models.BooleanField(default=False)

    finished_objects = FinishedManager()
    objects = models.Manager()


class TodoAdmin(admin.ModelAdmin):
    model = Todo

admin.site.register(Todo, TodoAdmin)

while having a Todo-class like below will return all objects:

class Todo(models.Model):
    title = models.CharField(max_length=250)
    finished = models.BooleanField(default=False)

    objects = models.Manager()
    finished_objects = FinishedManager()

Attachments (0)

Change History (3)

comment:1 Changed 5 years ago by mm

  • Cc martin@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 5 years ago by ramiro

This has been already reported in #9476, see that ticket for the rationale of why a code developer closed it as wontfix.

comment:3 Changed 5 years ago by jacob

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

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.