[Fixed]-Where do I modify the admin view code for a 3rd party app?

43👍

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.

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.

Leave a comment