分割“ admin.py” Django项目? [英] Splitting "admin.py" of a Django project?

查看:145
本文介绍了分割“ admin.py” Django项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将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:


  1. 创建一个名为 separated_admins 的目录,并放置一个空的 __ init __。py

  2. separated_admins 目录中创建文件,像这样的东西:

  1. make a directory named separated_admins and put an empty __init__.py in it
  2. 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 而不是 admin.py

    • 使用admin文件夹中的多个文件,例如您的 Some_Model_admin.py

    • 管理员 __ init __。py c>文件夹,然后将*所有文件导入其中。

    • 您可能还希望包含 __ all __ 提供一个干净的界面。

    • Create a folder called admin instead of admin.py
    • Use multiple files within the admin folder, like your Some_Model_admin.py
    • Create a __init__.py in the admin 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆