Remove default apps from Django-admin

Remove default apps from Django-admin

To remove or hide default apps from the Django admin interface, you can use the AdminSite class's unregister() method to unregister the models associated with those apps. Here's how you can do it:

  1. Create an admin.py file within the app where you want to remove the default apps or in your project's main admin.py file.

  2. Import the AdminSite class and the models you want to unregister.

  3. Use the unregister() method to unregister the models.

Here's an example of how you can remove the auth and sites apps (which include User and Group models) from the Django admin:

from django.contrib import admin from django.contrib.auth.models import User, Group from django.contrib.sites.models import Site # Unregister the models from the admin admin.site.unregister(User) admin.site.unregister(Group) admin.site.unregister(Site) 

Place this code in your admin.py file to unregister the models associated with the auth and sites apps. You can similarly unregister models from other apps you want to hide from the admin interface.

Keep in mind that unregistering models from the admin interface will hide them, but it won't delete them from the database. If you want to completely remove an app, you may also consider removing it from your project's INSTALLED_APPS setting in the settings.py file and then running makemigrations and migrate to clean up the database.

Examples

  1. "How to remove Django default apps from the admin site?"

    • This query demonstrates how to remove default Django applications from the admin interface.
    from django.contrib import admin from django.contrib.auth.models import Group # Unregister the default Group model admin.site.unregister(Group) # Removes the Group app from admin 
  2. "Remove default models from Django admin?"

    • This query shows how to unregister specific default models from the admin site.
    from django.contrib import admin from django.contrib.auth.models import User, Group # Unregister User and Group from the admin site admin.site.unregister(User) # Removes User model admin.site.unregister(Group) # Removes Group model 
  3. "Remove admin apps based on settings in Django?"

    • This query explores removing default apps from Django admin based on specific conditions in settings.
    from django.conf import settings from django.contrib import admin from django.contrib.auth.models import User, Group # Check if a setting to exclude default apps is True if getattr(settings, 'REMOVE_DEFAULT_APPS', False): admin.site.unregister(User) # Unregisters the User model admin.site.unregister(Group) # Unregisters the Group model 
  4. "Remove Django admin authentication for custom models?"

    • This query demonstrates how to remove Django's authentication app while keeping custom admin models.
    from django.contrib import admin from django.contrib.auth.models import User # Unregister the default User model but keep custom models admin.site.unregister(User) # Removes User model from the admin site 
  5. "Remove the admin login functionality in Django?"

    • This query discusses removing or customizing the admin login page.
    from django.contrib import admin from django.urls import path from django.http import HttpResponse # Custom admin view with no login def custom_admin_view(request): return HttpResponse("Custom Admin") urlpatterns = [ path('admin/', custom_admin_view), # Custom admin URL ] 
  6. "Unregister all default models in Django-admin?"

    • This query explores how to unregister all default models from the admin site.
    from django.contrib import admin from django.contrib.auth.models import User, Group, Permission # Unregister all common default models admin.site.unregister(User) # User admin.site.unregister(Group) # Group admin.site.unregister(Permission) # Permission 
  7. "Disable Django-admin for specific apps?"

    • This query shows how to disable Django admin for certain apps without affecting others.
    from django.apps import apps from django.contrib import admin # Get the app config by its label and unregister its models from admin app_config = apps.get_app_config('auth') # 'auth' app example for model in app_config.get_models(): admin.site.unregister(model) # Unregisters all models in the 'auth' app 
  8. "Remove Django admin index page apps?"

    • This query discusses customizing or removing apps from the Django admin index page.
    from django.contrib import admin class CustomAdminSite(admin.AdminSite): def index(self, request, extra_context=None): # Custom implementation that excludes certain apps from the index return super().index(request, extra_context=extra_context) # Override as needed admin.site = CustomAdminSite() # Replace the default admin site with the custom one 
  9. "Remove Django admin history for specific models?"

    • This query demonstrates how to remove or limit the history for specific admin models.
    from django.contrib import admin from django.contrib.auth.models import User # Custom model admin with no history class UserAdmin(admin.ModelAdmin): def has_view_permission(self, request, obj=None): return False # Disable history view admin.site.unregister(User) # Unregister the existing User model admin.site.register(User, UserAdmin) # Register custom User admin with no history 

More Tags

store static-members voice-recognition system.web.http sql-tuning time-complexity background-service transpiler string.h my.cnf

More Python Questions

More Everyday Utility Calculators

More Mixtures and solutions Calculators

More Financial Calculators

More Tax and Salary Calculators