Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I guess you have something like this at the top of your file:</p> <pre><code>from django.contrib.comments.admin import CommentAdmin </code></pre> <p>This import executes the registration of the model (at the very bottom of this admin file) <strong>again</strong>.</p> <p>One idea that doesn't look very nice (I actually haven't tried it) could be:</p> <pre><code>from django.contrib.comments.models import Comment from django.contrib import admin from django.contrib.admin.sites import NotRegistered # Try to unregister the Comment model # that was registered via the auto_discover method try: admin.site.unregister(Comment) except NotRegistered: pass # Now we can load the CommentAdmin (which reregisters the admin model) from django.contrib.comments.admin import CommentAdmin # We have to unregister again: try: admin.site.unregister(Comment) except NotRegistered: pass # Now your stuff... </code></pre> <p>I guess this could be done better but it should work. To make this approach work, the application that contains this file has to be after the comments application in <code>INSTALLED_APPS</code>.</p> <p>Now to your class. I think if you write <code>actions = ['ban_user']</code> you actually overwrite all the actions in the parent class. I think it is the easiest way to override the <code>get_actions</code> method:</p> <pre><code>class NewCommentAdmin(CommentAdmin): def get_actions(self, request): actions = super(NewCommentAdmin, self).get_actions(request) # Do some logic here based on request.user if you want # to restrict the new action to certain users actions.append('ban_user') return actions def ban_user(self, request, queryset): pass admin.site.register(Comment, NewCommentAdmin) </code></pre> <p>Hope that helps (or at least gives an idea) :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload