0

我将所有模型存储在一个名为 models 的文件夹中,它对我来说工作正常。但是 admin.py 有一些问题,我应该将这个文件存储在哪里?在模型文件夹或应用程序文件夹中?当我尝试将模型插入管理面板时出现错误。

错误:

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
4

1 回答 1

0

将对象分成模块到包等中是非常好的。您只需要记住,Django默认只在包的根目录中查找admin.pyand文件。models.py这里的解决方案是显式导入所需的文件,这样做的好地方是__init__首先编译的文件。

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

/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'
于 2013-03-16T09:10:59.530 回答