Django Newbie Model Error

As of Python 2.7.x + Django 1.9:

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

As a health check, I create a views.py file with a simple view that displays a hello world test message and a URL of this kind. I can run this with python manage.py runserver and it works fine.

I am creating a models.py file with one super simple Django ORM model class. FYI, my goal is to use existing tables and schema, so I don't want 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' 

Just adding import models to my views.py code leads to the following error when starting the server using python manage.py runserver :

"RuntimeError: model class simple.models.SuperSimpleModel does not declare an explicit app_label label and is not in the application in INSTALLED_APPS or was still imported before the application was loaded."

I assume my application is not initializing correctly? I digested this problem down to the above simple set of reproducible steps. In the steps above, I did not change anything in settings.py . Usually I need to set up a database, but I can reproduce the error without even doing it.

+5
source share
2 answers

You are correct in that you need to change the settings here. See this step of the Django tutorial for an example.

Judging by what you indicated here, it looks like you need to add 'simple' to the INSTALLED_APPS setting. Thus, the setup will look something like this:

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

Please note that 'simple' in itself may not be acceptable given how your PYTHONPATH is configured. You may need to add a more specific path to the application, as the above step is in the manual with 'polls.apps.PollsConfig' .

+8
source

I had a similar problem and it turned out that my interpreter had the wrong Python path settings. If the previous answer does not help, check it out. It should contain the path to the directory where manage.py is located.

0
source

Source: https://habr.com/ru/post/1238901/


All Articles