如何在 django 1.8 中使用 treebeard admin

How to use treebeard admin in django 1.8

我正在尝试在管理部分使用 django-treebeard,遵循 https://tabo.pe/projects/django-treebeard/docs/2.0/admin.html

中的建议

在 python 3.4.

上使用 django 1.8 和 treebeard 3.0

models.py:

class Site(models.Model):
    stype = models.IntegerField()
    name = models.CharField(max_length=255)


class SiteTree(NS_Node):
    site = models.ForeignKey(Site)

请注意,我需要以这种方式定义模型,因为一个站点可以位于层次结构中的多个点。我发现没有开箱即用的树库支持这一点,所以我只使用嵌套集和与实际数据的多对一关系。

我试过像这样将我的树添加到管理控制面板 (admin.py):

from django.contrib import admin
from .models import *
from treebeard.admin import admin_factory

# Register your models here.
admin.site.register((Site, SiteTree, admin_factory(SiteTree)))

我从中得到的是

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Python34\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "D:\Python34\lib\site-packages\django\core\management\__init__.py", line 312, in execute
    django.setup()
  File "D:\Python34\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Python34\lib\site-packages\django\apps\registry.py", line 115, in populate
    app_config.ready()
  File "D:\Python34\lib\site-packages\django\contrib\admin\apps.py", line 22, in ready
    self.module.autodiscover()
  File "D:\Python34\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "D:\Python34\lib\site-packages\django\utils\module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "D:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "D:\Android\Projects\ApartmentTerminal\ScantronServer\settings\admin.py", line 6, in <module>
    admin.site.register((SiteTree, admin_factory(SiteTree)))
  File "D:\Python34\lib\site-packages\django\contrib\admin\sites.py", line 85, in register
    if model._meta.abstract:
  AttributeError: type object 'SiteTreeAdmin' has no attribute '_meta'

如果我使用声明视图的继承方式没有任何改变。

我做错了什么?

您调用 admin.site.register 的方式无效 - 您已将所有参数包装在一个元组中,这意味着它们将作为一个参数传递给函数。试试这个:

admin.site.register(SiteTree, admin_factory(SiteTree))

或者您可以像这样手动创建管理员 class:

from django.contrib import admin
from .models import *
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory

class SiteTreeAdmin(TreeAdmin):
    form = movenodeform_factory(SiteTree)

# Register your models here.
admin.site.register(Site)
admin.site.register(SiteTree, SiteTreeAdmin)