Search code examples
pythondjango-rest-frameworkadmindjango-rest-authdjango-rest-viewsets

Using multiple admin.py files for Django rest?


I currently have 2 admin.py files.

  1. project/admin.py
  2. project/pages/basicpage/admin.py

I would like to use the registered classes inside the second admin.py along with the first admin.py such that they can both be reached at the same admin endpoint.

FILE ONE: project/admin.py

from django.contrib import admin
from project import models
from project.pages.basicpage import admin as BP_admin


@admin.register(models.Test)
class TestAdmin(admin.ModelAdmin):
    pass

FILE TWO: project/pages/basicpage/admin.py

from django.contrib import admin
from project import models

@admin.register(models.Platform)
class PlatformAdmin(admin.ModelAdmin):
    pass


@admin.register(models.Type)
class TypeAdmin(admin.ModelAdmin):
    pass

In file one, I have imported the second admin as BP_admin, not that it is used yet. However when I access my http://127.0.0.1:8000/admin endpoint, understandably I only can view the "Test" class that is registered in the first file. Any idea how to get my other 2 classes from my second file registered to the first file's endpoint?

Thanks!


Solution

  • Admin is just the models so importing the models should be enough. You can just add:

    from project.pages.basicpage import models as BP_models
    
    @admin.register(models.Test)
    ...
    
    @admin.register(BP_models.Platform)
    class Platform(models.Platform):
        pass
    

    You can also simplify and not use the class:

    @admin.register(models.Test, BP_models.Platform,....)