在Django admin中重写默认的queryset

时间:2022-12-02 19:18:15

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?

如何覆盖Django管理模块使用的默认queryset以包含已删除的对话?

4 个解决方案

#1


91  

You can override get_queryset method in your model admin class.

可以在模型管理类中覆盖get_queryset方法。

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.

注意,在Django<=1.5中,方法被命名为queryset。

#2


7  

Konrad is correct, but this is more difficult than the example given in the documentation.

Konrad是正确的,但是这比文档中给出的示例要困难得多。

Deleted conversations can't be included in a queryset that already excludes them. So I don't see an option other than re-implementing admin.ModelAdmin.queryset entirely.

已删除的对话不能包含在已经排除它们的queryset中。因此,除了重新实现admin.ModelAdmin之外,我看不到其他选项。queryset完全。

class ConversationAdmin (admin.ModelAdmin):

    def queryset (self, request):
        qs = Conversation.all_conversations
        ordering = self.get_ordering(request)
        if ordering:
            qs = qs.order_by(*ordering)
        return qs

#3


2  

What would be so wrong with the following:

下面这句话有什么不对呢?

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

So in your own apps/projects, you use Conversation.nondeleted_conversations() and let the built-in admin app do it's thing.

因此,在你自己的应用程序/项目中,你使用会话。nondeleted_conversations()并让内置的管理应用程序做到这一点。

#4


1  

The accepted solution works great for me but I needed a little bit more flexibility, so I ended up extending the changelist view to add in a custom queryset parameter. I can now configure my default queryset/filter as such and it can still be modified by using a different filter (get parameters):

接受的解决方案非常适合我,但我需要更大的灵活性,因此我最终扩展了changelist视图,以添加自定义的queryset参数。我现在可以配置我的默认queryset/filter,它仍然可以通过使用不同的过滤器(get参数)进行修改:

def changelist_view(self, request, extra_context=None):
    if len(request.GET) == 0 :
        q = request.GET.copy()
        q['status__gt'] = 4
        request.GET = q
        request.META['QUERY_STRING'] = request.GET.urlencode()

    return super(WorksheetAdmin,self).changelist_view(request, extra_context=extra_context)

#1


91  

You can override get_queryset method in your model admin class.

可以在模型管理类中覆盖get_queryset方法。

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.

注意,在Django<=1.5中,方法被命名为queryset。

#2


7  

Konrad is correct, but this is more difficult than the example given in the documentation.

Konrad是正确的,但是这比文档中给出的示例要困难得多。

Deleted conversations can't be included in a queryset that already excludes them. So I don't see an option other than re-implementing admin.ModelAdmin.queryset entirely.

已删除的对话不能包含在已经排除它们的queryset中。因此,除了重新实现admin.ModelAdmin之外,我看不到其他选项。queryset完全。

class ConversationAdmin (admin.ModelAdmin):

    def queryset (self, request):
        qs = Conversation.all_conversations
        ordering = self.get_ordering(request)
        if ordering:
            qs = qs.order_by(*ordering)
        return qs

#3


2  

What would be so wrong with the following:

下面这句话有什么不对呢?

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

So in your own apps/projects, you use Conversation.nondeleted_conversations() and let the built-in admin app do it's thing.

因此,在你自己的应用程序/项目中,你使用会话。nondeleted_conversations()并让内置的管理应用程序做到这一点。

#4


1  

The accepted solution works great for me but I needed a little bit more flexibility, so I ended up extending the changelist view to add in a custom queryset parameter. I can now configure my default queryset/filter as such and it can still be modified by using a different filter (get parameters):

接受的解决方案非常适合我,但我需要更大的灵活性,因此我最终扩展了changelist视图,以添加自定义的queryset参数。我现在可以配置我的默认queryset/filter,它仍然可以通过使用不同的过滤器(get参数)进行修改:

def changelist_view(self, request, extra_context=None):
    if len(request.GET) == 0 :
        q = request.GET.copy()
        q['status__gt'] = 4
        request.GET = q
        request.META['QUERY_STRING'] = request.GET.urlencode()

    return super(WorksheetAdmin,self).changelist_view(request, extra_context=extra_context)