分割“ admin.py” Django项目? [英] Splitting "admin.py" of a Django project?
问题描述
我正在尝试将Django项目的 admin.py
拆分为单独的文件,但失败了。
I'm trying to split admin.py
of a Django project into separate files but failed.
我没有从Google上找到有关如何拆分它的信息,所以我必须自己尝试一下。这是我尝试的方法:
There's no information I can find from google about how to split it, so I have to try myself. Here's what I tried:
- 创建一个名为
separated_admins
的目录,并放置一个空的__ init __。py
-
在
separated_admins
目录中创建文件,像这样的东西:
- make a directory named
separated_admins
and put an empty__init__.py
in it create files in
separated_admins
directory, something like this:
# file my_app/seperated_admins/Some_Model_admin.py
from my_app.models import Some_Model
from django.contrib import admin
admin.site.register(Some_Model)
在 admin.py
中,我添加了以下行:
in admin.py
, I added lines like:
from my_app.seperated_admins import *
但是我在管理站点中没有看到 Some_Model
。我的解决方案对吗?我该如何解决?
But I didn't see Some_Model
in my admin site. Is my solution right? How can I fix this?
推荐答案
admin
只是一个Python模块。因此,正确的拆分方法如下:
The admin
is just a python module. So the right way of splitting it would be as follows:
- 创建一个名为
admin $ c $的文件夹c>而不是
admin.py
- 使用admin文件夹中的多个文件,例如您的
Some_Model_admin.py
- 在
管理员$ c $中创建
__ init __。py
c>文件夹,然后将*所有文件导入其中。 - 您可能还希望包含
__ all __
提供一个干净的界面。
- Create a folder called
admin
instead ofadmin.py
- Use multiple files within the admin folder, like your
Some_Model_admin.py
- Create a
__init__.py
in theadmin
folder and import * all the files into it. - You might also want to include an
__all__
to provide a clean interface.
这篇关于分割“ admin.py” Django项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!