Newbie Django Model Error

橙三吉。 提交于 2019-12-07 04:56:21

问题


With Python 2.7.x + Django 1.9:

I create a new super-simple Django skeleton project with django-admin startproject simple

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.

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'

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

"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."

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.


回答1:


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

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',
]

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




回答2:


I faced a similar problem, and it turned out that my interpreter had wrong Python path settings. If the previous answer doesn't help, check it out. It should contain path to the directory your manage.py is on.



来源:https://stackoverflow.com/questions/34406369/newbie-django-model-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!