Override default queryset in Django admin

前端 未结 6 2076
孤城傲影
孤城傲影 2020-11-27 14:51

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

class NondeletedManager(models.Manager):
    \"\"\"Returns only objects which ha         


        
6条回答
  •  借酒劲吻你
    2020-11-27 15:25

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

    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.

    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
    

提交回复
热议问题