[Fixed]-Django – Extending another apps ModelAdmin?

4πŸ‘

βœ…

I guess you have something like this at the top of your file:

from django.contrib.comments.admin import CommentAdmin

This import executes the registration of the model (at the very bottom of this admin file) again.

One idea that doesn’t look very nice (I actually haven’t tried it) could be:

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...

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 INSTALLED_APPS.

Now to your class. I think if you write actions = ['ban_user'] you actually overwrite all the actions in the parent class. I think it is the easiest way to override the get_actions method:

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)

Hope that helps (or at least gives an idea) πŸ™‚

πŸ‘€Felix Kling

12πŸ‘

Here’s how I do it in one project for the User model. In the admin.py for my app:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    # ...

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
πŸ‘€Carl Meyer

5πŸ‘

Unregister the Comment model first.

πŸ‘€ozan

0πŸ‘

Have a look at https://github.com/kux/django-admin-extend

It offers some easy to use functions and decorators that implement the functionality you’re requesting in a very flexible manner. The documentation does a pretty good job at explaining why using this approach is better than direct inheritance.

It also has support for injecting bidirectional many to many fields.

Leave a comment