Difference between admin.register and admin.site.register
I'm writing an API in Django and I show its metrics in a view on Django Admin. My Django project has more than one app, and in one of them is the view that feeds information for the metrics template.
I used:
from django.contrib import admin
...
admin.register(ApiRequestLog, ApiRequestLogAdmin)
where ApiRequestLog is a model that I use to log every request user, time, etc.
But the endpoint didn't show in uv run manage.py show_urls, nor it was available to be linked in other templates: my index.html had a button referencing to it and crashed by not finding the reverse URL.
Then I changed to admin.site.register(ApiRequestLog, ApiRequestLogAdmin) and it worked. As searched online, admin.register is an alias for admin.site.register. Should I keep using admin.site.register or the shorter version?
admin.register is a decorator. Likely you were calling it as a function, which is why admin.site.register worked (it is a function!). It is recommended to use admin.register. The django documentation features this example:
from django.contrib import admin
from .models import Author, Editor, Reader
from myproject.admin_site import custom_admin_site
@admin.register(Author, Reader, Editor, site=custom_admin_site)
class PersonAdmin(admin.ModelAdmin):
pass
If you need any more information, the django documentation has examples of it being used.