Django管理面板的项目问题

时间:2013-03-16 08:29:13

标签: django django-models

我将所有模型存储在一个名为models的文件夹中,它对我来说很好。但是admin.py存在一些问题,我应该在哪里存储这个文件?在模特文件夹内或app文件夹中?当我试图将模型插入管理面板时,我收到了错误。

错误:

TypeError at /admin/

'module' object is not iterable

Request Method:     GET
Request URL:    http://localhost:9004/admin/
Django Version:     1.4.5
Exception Type:     TypeError
Exception Value:    

'module' object is not iterable

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py in register, line 74
Python Executable:  /usr/bin/python2.7
Python Version:     2.7.3
Python Path:    

['/home/***/**/**/Testcase_Project',
'/home/***/**/**/Testcase_Project',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
'/usr/lib/python2.7/dist-packages/ubuntuone-couch',
'/usr/lib/python2.7/dist-packages/ubuntuone-installer',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']

Server time:    Sat, 16 Mar 2013 11:31:39 +0200
Traceback Switch to copy-and-paste view

/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response

                                request.path_info)

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in resolve

                for pattern in self.url_patterns:

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in url_patterns

            patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in urlconf_module

                self._urlconf_module = import_module(self.urlconf_name)

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module

        __import__(name)

    ...
▶ Local vars
/home/***/***/***/Testcase_Project/Testcase_Project/urls.py in <module>

    admin.autodiscover()

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py in autodiscover

                import_module('%s.admin' % app)

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module

        __import__(name)

    ...
▶ Local vars
/home/***/***/***/Testcase_Project/Testcase_Project/admin.py in <module>

    admin.site.register(test_cases)

    ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py in register

            for model in model_or_iterable:

    ...
▶ Local vars

1 个答案:

答案 0 :(得分:0)

将对象分成模块到包等是完全没问题的。您只需要记住,Django默认只在包的根中查找admin.pymodels.py文件。这里的解决方案是显式导入所需的文件,这是首先编译的__init__文件的好地方。

所以,例如(Django 1.4设置,关于Django结构的更多信息here):

/project_root
    /project
        __init__.py
        settings.py
        urls.py
    /app
        __init__.py
        admin.py
        models.py
    manage.py

可以转换为:

/project_root
    /project
        __init__.py
        settings.py
        urls.py
    /app
        __init__.py  # <- import statements
        /admin
            admin_foo.py
            admin_bar.py
        /models
            model_foo.py
            model_bar.py
    manage.py

然后在您的应用文件夹中的__init__.py内添加:

from .models.model_foo import Foo
from .models.model_bar import Bar
from .admin.admin_foo import FooAdmin
from .admin.admin_bar import BarAdmin

由于安装的应用程序如何定义内容,您不应忘记将app_label属性添加到模型Meta类中:

class Foo(models.Model):
    # fields...
    class Meta:
        app_label = 'app'


class Bar(models.Model):
    # fields...
    class Meta:
        app_label = 'app'