andilabs
andilabs

Reputation: 23361

Can I add multiple models from different apps to one admin site?

Can I add multiple models from different apps to one admin site? I would like see e.g both apps (polls, news) models under one roof of admin panel.

For now in my Django admin site NONE of the models is displayed.

I try in admin.py file

from tutorial.polls.models import Poll, Choice
from tutorial.news.models import Article, Reporter
from django.contrib import admin

admin.site.register(Poll)
admin.site.register(Choice)
admin.site.register(Article)
admin.site.register(Reporter)

My catalogs structure:

(example)andi@MacBook-Pro-Andrzej:~/example/tutorial$ ls -lR
total 8
-rw-r--r--   1 andi  staff   251B 28 paź 11:53 manage.py
drwxr-xr-x  12 andi  staff   408B 31 paź 02:46 news/
drwxr-xr-x   9 andi  staff   306B 31 paź 02:46 polls/
drwxr-xr-x  12 andi  staff   408B 31 paź 03:06 tutorial/


    ./news:
    total 64
    -rw-r--r--  1 andi  staff     0B 31 paź 00:18 __init__.py
    -rw-r--r--  1 andi  staff   210B 31 paź 02:09 base.html
    -rw-r--r--  1 andi  staff   466B 31 paź 01:26 models.py
    -rw-r--r--  1 andi  staff   383B 31 paź 00:18 tests.py
    -rw-r--r--  1 andi  staff   277B 31 paź 02:08 views.py
    -rw-r--r--  1 andi  staff   329B 31 paź 01:53 year_archive.html

    ./polls:
    total 40
    -rw-r--r--  1 andi  staff     0B 31 paź 02:32 __init__.py
    -rw-r--r--  1 andi  staff   612B 31 paź 02:33 models.py
    -rw-r--r--  1 andi  staff   383B 31 paź 02:32 tests.py
    -rw-r--r--  1 andi  staff    26B 31 paź 02:32 views.py

    ./tutorial:
    total 72
    -rw-r--r--  1 andi  staff     0B 28 paź 11:53 __init__.py
    -rw-r--r--  1 andi  staff   244B 31 paź 03:06 admin.py
    -rw-r--r--  1 andi  staff   5,4K 31 paź 02:34 settings.py
    -rw-r--r--  1 andi  staff   752B 31 paź 02:36 urls.py
    -rw-r--r--  1 andi  staff   1,4K 28 paź 11:53 wsgi.py

Upvotes: 0

Views: 1502

Answers (1)

Christian Ternus
Christian Ternus

Reputation: 8482

Welcome to Django programming!

Easy! Create an admin.py in polls/ and news/, and only include the models you need for that app.

For example, polls/admin.py might contain:

from models import Poll, Choice
from django.contrib import admin

admin.site.register(Poll)
admin.site.register(Choice)

That ought to work just fine.

(I believe your current approach isn't working because polls and news aren't reachable from within tutorial/admin.py, and tutorial.polls.models can't find them -- polls/ and models/ would need to be subdirectories of tutorial/ for that to work. Otherwise you're on the right track -- keep going! :) )

Upvotes: 2

Related Questions