Override default queryset in Django admin

One of my models has a deleted flag, which is used to hide objects globally:

class NondeletedManager(models.Manager):
    """Returns only objects which haven't been deleted"""

    def get_query_set(self):
        return super(NondeletedManager, self).get_query_set().exclude(deleted=True)

class Conversation(BaseModel):
    ...
    deleted = models.BooleanField(default=False)
    objects = NondeletedManager()
    all_conversations = models.Manager() # includes deleted conversations

How can I override the default queryset used by Django admin module to include deleted conversations?

Answers


You can override get_queryset method in your model admin class.

class MyModelAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(MyModelAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)

Note in Django<=1.5 the method was named just queryset.


Need Your Help

Flex custom component, best way to use it

actionscript-3 flex flex4.5 custom-component

I have not completly understood how custom components work...

Disclosure “i” button or chevron for iPhone app?

iphone user-interface uitableview cocoa-design-patterns

I have a UITableView with rows that each have two actions that can be performed. The user can be shown the item details screen (edit the name and see summary info) or they can 'open' the item to pr...