gpt4 book ai didi

python - 如何在 django 1.8 中使用 Treebeard 管理

转载 作者:太空宇宙 更新时间:2023-11-03 17:20:40 32 4
gpt4 key购买 nike

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

在 python 3.4 上使用 django 1.8 和 Treebeard 3.0。

模型.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'

如果我使用继承方式声明 View ,则不会发生任何变化。

我做错了什么?

最佳答案

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

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

或者,您可以像这样手动创建管理类:

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)

关于python - 如何在 django 1.8 中使用 Treebeard 管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33196771/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com