【问题标题】:Django Nested Admin returns 404 or doesn't inline models in django admin areaDjango Nested Admin 返回 404 或不在 django 管理区域内联模型
【发布时间】:2019-12-21 16:42:39
【问题描述】:

我正在尝试让django nested admin 工作,但我遇到了一些问题,我确定我只是犯了一个愚蠢的错误。以下是我遵循的步骤:

第 1 步:我进行了 pip 安装

第 2 步:我将其添加到我的 settings.py 中已安装应用程序的底部

第 3 步:我将它添加到我的 URL 数组中:

他们的例子:

urlpatterns = patterns('',
# ...
url(r'^_nested_admin/', include('nested_admin.urls')),
)

我的实现:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include("estimate_maker.urls")),
    path('nested_admin/', include('nested_admin.urls')),
]

第 4 步:我在 settings.py 中创建了一个静态文件夹

第 5 步:我运行了 collectstatic 命令

第 6 步:我在项目文件夹中设置了 admin.py:

from django.contrib import admin
from .models import MoldInspection, MoldService
import nested_admin


class MoldServiceInline(nested_admin.NestedStackedInline):
    model = MoldService


class MoldInspectionInline(nested_admin.NestedModelAdmin):
    model = MoldService
    sortable_field_name = "position"
    inlines = [MoldServiceInline]


admin.site.register(MoldInspection)
admin.site.register(MoldService)

我确实收到了来自 pycharm 的警告,说我不确定如何诊断,因为我正在按照指南中的方式设置课程。

Cannot find reference 'NestedModelAdmin' in '__init__.py'

查看引用的 __init__.py 我看到了:

# import mapping to objects in other modules
all_by_module = {
    'nested_admin.forms': (
        'SortableHiddenMixin'),
    'nested_admin.formsets': (
        'NestedInlineFormSet', 'NestedBaseGenericInlineFormSet'),
    'nested_admin.nested': (
        'NestedModelAdmin', 'NestedModelAdminMixin', 'NestedInlineAdminFormset',
        'NestedInlineModelAdmin', 'NestedStackedInline', 'NestedTabularInline',
        'NestedInlineModelAdminMixin', 'NestedGenericInlineModelAdmin',

但是当我将我的 admin.py 更新为:

class MoldInspectionInline(nested_admin.nested.NestedModelAdmin):

我得到同样的错误,这次指向“嵌套”。

当我尝试通过转到 /nested-admin 来访问嵌套管理员时,我只会收到 404 错误消息:

Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:

admin/
[name='home']
nested-admin ^server-data\.js$ [name='nesting_server_data']

当我转到 /admin 时,它看起来和以前一样。

更多细节:

我希望我的 MoldService 仅作为子服务的父级存在,因此我将其设置如下:

class MoldService(models.Model):
    title = "Mold Services"
    def __str__(self):
        return self.title

然后我将我的子类设置如下:

class MoldInspection(models.Model):
    title = "Mold Inspection"
    description = models.TextField(null=True)

    def __str__(self):
        return self.description

为什么你认为嵌套管理员不适合我?

【问题讨论】:

    标签: django django-admin django-apps


    【解决方案1】:

    在引用的__init__.py 文件中包含以下注释

    # All this craziness is so that we can allow the classes in nested_admin.formsets

    # to be importable directly from this module

    我猜是这里发生的事情是问题,所以我只是删除了__init__.py的内容,然后用

    导入

    from nested_admin.nested import *

    现在可以使用了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-25
      • 2021-08-18
      • 2017-10-26
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多