15

I am using a 3rd party app in my django webapp. But I want to customize the admin view for one of the models in the 3rd party app. The customization is more than changing the change_list.html template i.e. I will need to add code to talk to an external webservice etc.

However, I don't want to modify the 3rd party app. Instead I want to override it. How I override the ModelAdmin for a model that comes from a 3rd party app ?

canadadry
  • 8,115
  • 12
  • 51
  • 68

3 Answers3

42

This should get you started:

from django.contrib import admin
from thirdpartyapp.models import ThirdPartyModel
from thirdpartyapp.admin import ThirdPartyAdmin

class CustomThirdPartyAdmin(ThirdPartyAdmin):
    pass


admin.site.unregister(ThirdPartyModel)
admin.site.register(ThirdPartyModel, CustomThirdPartyAdmin)

I use this often to customize the UserAdmin as shown in this answer.

Community
  • 1
  • 1
Trey Hunner
  • 10,975
  • 4
  • 55
  • 114
2

Several years later, for those experiencing this issue, the solution is in this link .

However fore newbies like me, a little bit more explanation on how to implement it is in place.

First, the issue happened in Postgres (at least to me) and not in Sqlite, for some reason I was not able to delete users in Postgres, furthermore this started happening after I set up an email server and set up the user validation with Djoser through email. I was not able to find the root cause, there are some really old bugs pointing to this issue, but they exceed me.

The solution is in the linked answer:

from rest_framework_simplejwt import token_blacklist

class OutstandingTokenAdmin(token_blacklist.admin.OutstandingTokenAdmin):

    def has_delete_permission(self, *args, **kwargs):
        return True # or whatever logic you want

admin.site.unregister(token_blacklist.models.OutstandingToken)
admin.site.register(token_blacklist.models.OutstandingToken, OutstandingTokenAdmin)

This code should be inside the app you created for your users inside the admin.py file. It shouldn't be in the rest_framework token blacklist admin.py file.

If you face the 'rest_framework_simplejwt.token_blacklist' has no attribute 'admin' issue, it is simply because you are importing incorrectly.

What I did was change the import statement to work with my code as follows:

from rest_framework_simplejwt.token_blacklist import admin as tokadmin
from rest_framework_simplejwt.token_blacklist import models as tokmodels
class OutstandingTokenAdmin(tokadmin.OutstandingTokenAdmin):

def has_delete_permission(self, *args, **kwargs):
    return True # or whatever logic you want

admin.site.unregister(tokmodels.OutstandingToken)
admin.site.register(tokmodels.OutstandingToken, OutstandingTokenAdmin)
0

For those who after put the code on any app/admin.py file, and get this error:

django.contrib.admin.sites.NotRegistered: The model <MODEL_APP_NAME> is not registered

You need to change the name of the admin view for example adding "Custom<Model_NAME>Admin" I think there are conflicts using the same model admin name. With that solution removes the error.

xtornasol512
  • 2,399
  • 1
  • 12
  • 7