Override default request in Django admin

One of my models has a remote flag that is used to hide objects around the world:

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 query set used by the Django admin module to enable remote chains?

+55
django django-models django-admin
Sep 10
source share
5 answers

You can override the get_queryset method in the admin class of your model.

 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 that in Django <= 1.5, the method was simply called queryset .

+102
Sep 10
source share

Konrad is right, but it is more complicated than the example given in the documentation.

Remote conversations cannot be included in the query set, which already excludes them. Therefore, I see no other option than reimplementing admin.ModelAdmin.queryset completely.

 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 
+7
Sep 10
source share

What would be 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 applications / projects, you use Conversation.nondeleted_conversations() and let the built-in admin application do this.

+2
Sep 10
source share

The solution worked fine for me, but I needed a little more flexibility, so I expanded the list of changes to add a query set to the user parameter. Now I can configure my default set / filter as such, and it can be changed using another filter (get parameters):

 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
Sep 04 2018-11-14T00:
source share

You can do this with the Django proxy model .

 # models.py class UnfilteredConversation(Conversation): class Meta: proxy = True # this will be the 'default manager' used in the Admin, and elsewhere objects = models.Manager() # admin.py @admin.register(UnfilteredConversation) class UnfilteredConversationAdmin(Conversation): # regular ModelAdmin stuff here ... 

Or, if you have a ModelAdmin class that you want to reuse:

 admin.site.register(UnfilteredConversation, ConversationAdmin) 

This approach avoids the problems that might arise when overriding the default dispatcher in the original conversation model, since the default dispatcher is also used in ManyToMany relationships and inverse ForeignKey relationships.

0
Feb 01 '19 at 0:36
source share



All Articles