clay clay - 2 years ago 79
Python Question

Newbie Django Model Error

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
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 runserver
and it works fine.

I create a
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
code causes the following error to happen on server startup with
python 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
in the above steps. Normally, I would need to configure the database, but I can reproduce the error without even doing that.

Answer Source

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:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download