Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,....)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...