After I added the JWT functionality to my REST apis built using djangorestframework
, I have noticed these two models (BlacklistedToken
and OutstandingToken
) on my django admin panel that in the django admin panel, which I want to hide:
I already checked this question: Hide the token table from the admin panel in Django REST Framework
Which is quite similar to the issue I have, except that since I'm using djangorestframework-simplejwt
I did the following on my admins.py
:
from django.contrib import admin
from rest_framework_simplejwt.token_blacklist.models import (
BlacklistedToken,
OutstandingToken,
)
# Unregister the blacklisted and outstanding token models
admin.site.unregister(BlacklistedToken)
admin.site.unregister(OutstandingToken)
and I get:
File "/home/couzhei/code/Map-of-defect/backend/venv/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 153, in unregister
raise NotRegistered("The model %s is not registered" % model.__name__)
django.contrib.admin.exceptions.NotRegistered: The model BlacklistedToken is not registered
I even intentionally register
and then unregister
them, but it only removes the above exception, not hiding them. Any ideas would be appreciated.
There was no error in my django project.
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework_simplejwt.token_blacklist',
'authentication',
'test',
]
# myapp.admin.py
from django.contrib import admin
from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken, OutstandingToken
admin.site.unregister(BlacklistedToken)
admin.site.unregister(OutstandingToken)
The unregister method causes a NotRegistered error if the class you want to delete does not exist in the _registry property of the AdminSite class object.
class AdminSite:
...
def unregister(self, model_or_iterable):
"""
Unregister the given model(s).
If a model isn't already registered, raise NotRegistered.
"""
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
if model not in self._registry:
raise NotRegistered("The model %s is not registered" % model.__name__)
del self._registry[model]
...
Simply an error that occurs when you try to delete something that has not yet been registered. The register is associated with the order of INSTALLED_APPS(settings.py).
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'authentication',
'rest_framework_simplejwt.token_blacklist',
'test',
]
Unlike before, an error occurs when the app authentication containing the code to perform admin.site.unregister (BlacklistedToken) is placed above 'rest_framework_simplejwt.token_blacklist'.
Because the admin in the authentication(myapp) app may be performed before the admin 'rest_framework_simplejwt.token_blacklist' admin to delete it before registering it.
# djangorestframework-simplejwt/rest_framework_simplejwt/token_blacklist/admin.py
...
admin.site.register(OutstandingToken, OutstandingTokenAdmin)
...
admin.site.register(BlacklistedToken, BlacklistedTokenAdmin)
...
I wonder if an error has occurred because of this problem.