我正在尝试在管理部分使用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'
如果我使用继承方式声明视图,那么什么都不会改变。
我做错了什么?
答案 0 :(得分:1)
您调用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)