Django 管理站点 - 放置 admin.py 的位置

发布于 2025-01-17 17:07:56 字数 1088 浏览 3 评论 0原文

我在工作中继承了Django应用程序,并试图做出一些改进。该应用程序正在毫无问题地工作,但是直接在数据库表中进行了一些维护工作。我想设置管理站点,以免直接编辑数据库表。

我对Django的经验很少,并且已经通过了很多教程,但是这种应用结构实际上并不与我以前见过的任何相匹配。

该应用程序是使用DJANGO 3.2.9(来自Enesuption.txt)构建的,生产版由MySQL CloudSQL实例托管在App Engine上的GCP中。我目前正在通过Manage.py Runserver(使用本地MySQL实例)在本地计算机上进行测试。在某个时候,我将更新Django的版本。

我的问题是,该代码是构造的

project/  
|---manage.py  
|---requirements.txt  
+---project_app/  
|----|----settings.py  
|----|----urls.py  
+---app1/  
|---+---models/  
|----|---+---app1/  
|----|----|----|----app1.py  
|---+---urls/  
|----|---+---app1/  
|----|----|----|----routing.py  
|---+---views/  
|----|---+---app1/  
|----|----|----|----view.py  
+---app2  
|---+---similar structure to app1  
+---app3  
|---+---similar structure to app1  
+---app4  
|---+---similar structure to app1  

,我将在App1应用程序中创建哪个文件夹,以便导入管理站点进行管理的模型?

管理站点正在加载,并在列表中显示了用户和组表(以及Python Social Auth和Auth Tokens表)。

关于管理站点如何找到Admin.py文件,我几乎无法找到文档。我还尝试使用ProcMon(Windows)来查看它是否尝试在任何文件夹中查找Admin.py。除了VENV中的站点包装之外,Procmon没有在任何Admin.py文件上看到任何读取。

感谢您的帮助

I inherited a Django application at work and I am attempting to make some improvements. The application is working without issue however there is some maintenance work that is being done directly in the database tables. I would like to setup the Admin site to avoid having to directly edit the database table(s) content.

I have minimal experience with Django and have been thru a lot of the tutorials however this application structure doesn't really match any I have seen before.

The application is built using Django 3.2.9 (from the requirements.txt) The production version is hosted in GCP on App Engine with a MySQL CloudSQL instance. I am currently testing on a local machine via manage.py runserver(using a local MySQL instance.) At some point I will be updating versions of Django.

The code is structured like this

project/  
|---manage.py  
|---requirements.txt  
+---project_app/  
|----|----settings.py  
|----|----urls.py  
+---app1/  
|---+---models/  
|----|---+---app1/  
|----|----|----|----app1.py  
|---+---urls/  
|----|---+---app1/  
|----|----|----|----routing.py  
|---+---views/  
|----|---+---app1/  
|----|----|----|----view.py  
+---app2  
|---+---similar structure to app1  
+---app3  
|---+---similar structure to app1  
+---app4  
|---+---similar structure to app1  

My question is, which folder would I create the admin.py inside the app1 application in order to import the models for the Admin site to manage?

The Admin site is loading and shows the Users and Groups tables (along with the Python Social Auth and Auth Tokens tables) in the list.

There is very little documentation I could find about how the Admin site locates admin.py files. I have also tried using Procmon (Windows) to see if it attempts to find admin.py in any folders. Procmon is not seeing any reads on any admin.py file except under the site-packages in the venv.

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2025-01-24 17:07:57

更新:能够解决这个

TL;DR admin.py 进入与“导入管理”行相同的文件夹。
添加

from django.contrib import admin

到 app1 中的 app.py 并在同一文件夹中包含 admin.py 并进行管理配置确实有效

project/  
|---manage.py  
|---requirements.txt  
+---project_app/  
|----|----settings.py  
|----|----urls.py  
+---app1/  
|----app.py  
|----admin.py  
|---+---models/  
|----|----models_file.py
|---+---urls/  
|----|---+---app1/  
|----|----|----|----routing.py  
|---+---views/  
|----|---+---app1/  
|----|----|----|----view.py  
+---app2  
|---+---similar structure to app1  

一些额外的事情

  1. 如果您想自定义管理站点模型,INSTALLED_APPS 的顺序很重要
    我想从管理中删除social_django模型,这需要将social_django和social_core放在INSTALLED_APPS中的app1之上

  2. 此应用程序使用Django REST框架和rest_framework_simplejwt包,其中包括令牌模型,默认情况下在管理中显示。确保 DRF 和 DRF jwt 位于 INSTALLED_APPS 中的 app1 之前,而不是可以在 admin.py 中取消注册

最终结果是一个如下所示的 admin.py 文件

from django.contrib import admin

from django.contrib.auth.models import User, Group
from social_django.models import Association, Nonce, UserSocialAuth
from rest_framework.authtoken.models import TokenProxy
# My app1 models Imports

# Remove Django REST Framework models from admin.  
admin.site.unregister(TokenProxy)

# Remove Default user and group models provided by Django
admin.site.unregister(User)
admin.site.unregister(Group)

# Remove Social Auth models from social-auth-app-django package
admin.site.unregister(Association)
admin.site.unregister(Nonce)
admin.site.unregister(UserSocialAuth)

# My app1 models site.unregister

Update: Was able to solve this

TL;DR the admin.py goes into same folder you have the "import admin" line in.
Adding

from django.contrib import admin

into app.py in app1 and having an admin.py in the same folder and making admin configuration does work

project/  
|---manage.py  
|---requirements.txt  
+---project_app/  
|----|----settings.py  
|----|----urls.py  
+---app1/  
|----app.py  
|----admin.py  
|---+---models/  
|----|----models_file.py
|---+---urls/  
|----|---+---app1/  
|----|----|----|----routing.py  
|---+---views/  
|----|---+---app1/  
|----|----|----|----view.py  
+---app2  
|---+---similar structure to app1  

Some additional things

  1. The order of INSTALLED_APPS matters if you want to customize the admin site models
    I wanted to remove the social_django models from the admin and that required putting social_django and social_core above app1 in INSTALLED_APPS

  2. This application is using Django REST Framework with the rest_framework_simplejwt package which includes a Tokens model and this shows by default in the admin. Make sure the DRF and DRF jwt is before app1 in the INSTALLED_APPS than it can be unregistered in admin.py

End result is a admin.py file that looks like this

from django.contrib import admin

from django.contrib.auth.models import User, Group
from social_django.models import Association, Nonce, UserSocialAuth
from rest_framework.authtoken.models import TokenProxy
# My app1 models Imports

# Remove Django REST Framework models from admin.  
admin.site.unregister(TokenProxy)

# Remove Default user and group models provided by Django
admin.site.unregister(User)
admin.site.unregister(Group)

# Remove Social Auth models from social-auth-app-django package
admin.site.unregister(Association)
admin.site.unregister(Nonce)
admin.site.unregister(UserSocialAuth)

# My app1 models site.unregister
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文