Home > OS >  Import models from different apps to admin Django
Import models from different apps to admin Django

Time:12-02

I'm trying to create an admin page for my project including app1 and app2

myproject
    settings.py
    urls.py
    admin.py
app1
app2

In myproject/urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app1/', include('app1.urls')),
    path('app2/', include('app2.urls')),
    
]

In myproject/admin.py

from django.contrib import admin
from app1.models import User
from app2.models import Manager, Employee, Task, Template


admin.site.register(User)
admin.site.register(Manager)
admin.site.register(Employee)
admin.site.register(Task)
admin.site.register(Template)

Why doesn't my admin page import any models at all? Thanks!

CodePudding user response:

Every app should have their own admin.py file by default design. Django will detect it in the respective app folder and reflect it in admin page.

However if what you're trying here is to compile every model in one admin.py. Django wont detect that file by default. Another way to do this, you can try create a new app "admin" and register every model in that folder. I never did this because admin.py separated in their own app has its own very significant purpose.

CodePudding user response:

inside each app you must put admin file so can django track these files , so in your app1 in admin.py file related to app1 directory app1/admin.py , you need to put this code

from django.contrib import admin
from app1.models import User
 
admin.site.register(User)

and in app2 in admin.py related to app2 directoryapp2/admin.py put this :


from django.contrib import admin

from app2.models import Manager, Employee, Task, Template


admin.site.register(Manager)
admin.site.register(Employee)
admin.site.register(Task)
admin.site.register(Template)
  • Related