新手Django模型错误 [英] Newbie Django Model Error

查看:148
本文介绍了新手Django模型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python 2.7.x + Django 1.9:

With Python 2.7.x + Django 1.9:

我使用 django-admin startproject simple <<创建了一个新的超简单Django框架项目,

作为健全性检查,我创建了一个具有简单视图的 views.py 文件会输出 hello world类型的测试消息和指向该视图的网址路由。我可以使用 python manage.py runserver 运行它,并且工作正常。

As a sanity check, I create a views.py file with a simple view that outputs a "hello world" type test message and a url route to that view. I can run this with python manage.py runserver and it works fine.

我创建了 models.py 文件和一个超级简单的Django ORM模型类。仅供参考,我的目标是使用现有的表和模式,所以我不希望ORM生成新表。

I create a models.py file with a single super simple Django ORM model class. FYI, my goal is to use existing tables and schema, so I don't want the ORM to generate new tables.

class SuperSimpleModel(models.Model):
    some_value = models.CharField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'model_test_table'

只向我添加导入模型 views.py 代码导致使用 python manage.py runserver 的服务器启动时发生以下错误:

Merely adding import models to my views.py code causes the following error to happen on server startup with python manage.py runserver:


RuntimeError:模型类simple.models.SuperSimpleModel没有声明显式的app_label,也不是INSTALLED_APPS中的应用程序中的
或否则在
应用程序加载之前就已导入。

"RuntimeError: Model class simple.models.SuperSimpleModel doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded."

我认为我的应用程序未正确初始化?我把这个问题归结为上述简单的可重复步骤。在上述步骤中,我没有更改 settings.py 中的任何内容。通常,我将需要配置数据库,但是我什至可以重做该错误。

I presume that my application isn't initializing correctly? I have boiled this problem down to the above simple set of reproducible steps. I haven't changed anything in settings.py in the above steps. Normally, I would need to configure the database, but I can reproduce the error without even doing that.

推荐答案

您需要在此处修改设置。例如,请参见此Django教程步骤

You are correct in that you need to modify the settings here. As an example, see this Django tutorial step.

从您在此处提供的内容来看,您似乎必须在'simple'中添加您的 INSTALLED_APPS 设置。这样设置最终看起来像这样:

Judging from what you've provided here, it looks like you're going to have to add 'simple' to your INSTALLED_APPS setting. So that setting would end up looking something like this:

INSTALLED_APPS = [
    'simple',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

请注意,鉴于您的PYTHONPATH的设置方式,单独使用'simple'可能不合适。您可能需要为应用程序添加一个更特定的路径,就像上面的教程步骤使用’polls.apps.PollsConfig’一样。

Note that 'simple', by itself, may not be appropriate, given how your PYTHONPATH is set up. You may need to add a more specific path to the application, as the above tutorial step did with 'polls.apps.PollsConfig'.

这篇关于新手Django模型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆