Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 632
clay Avatar asked Dec 21 '15 23:12

clay


1 Answers

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

like image 196
Joey Wilhelm Avatar answered Nov 14 '22 12:11

Joey Wilhelm