2

I have spent countless hours but this is still stuck. The documentation is so lacking. Using Django 1.10, trying to create Sphinx documentation which has been giving various errors. Finally I am stuck here. I created an example model in my main app kyc_connect as below.

Models.py

from django.db import models
class example(models.Model):        
    filed1 = models.DateTimeField(auto_now=True)

    # class Meta:
    #     app_label = 'kyc_connect'

Running make_html gives the below error.

RuntimeError: Model class kyc_connect.models.example doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Conf.py import settings

import os
import sys    
sys.path.insert(0, os.path.abspath('..'))
from django.conf import settings
settings.configure()
import django
django.setup()

When I include Meta class presently commented out, this errors goes away. But if I include a model with ForeignKey and import from django.contrib.auth.models import User it gives error RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework_swagger',
    'rest_framework',
    'rest_framework.authtoken',
    'kyc_connect',
    'kyc_connect_data_models',
    'kyc_rest_services.kyc_connect_accounts',
    'kyc_rest_services.kyc_connect_documents',
    'kyc_rest_services.kyc_connect_transaction_manager',
    'tasks',
    'elasticstack',
    'corsheaders',
    'haystack'
]

ProjectStructure

kyc_connect:
  -config
  -docs
  -kyc_connect
    -models.py
    .
    .
  -kyc_connect_data_models
  -kyc_core
  -kyc_rest_services
    -kyc_connect_accounts
    -kyc_connect_transaction_manager
    .
    .
  .
  .

I already have django.contrib.contentype there. But django doesn't seem to understand. I don't want to declare meta class. What is going wrong. Any help would be great.

mzjn
  • 48,958
  • 13
  • 128
  • 248
garg10may
  • 5,794
  • 11
  • 50
  • 91
  • Did you run `makemigratoin` and `migrate`? – Max M Sep 07 '17 at 08:40
  • No, I am not even aware how it is dependent on that, let me try. – garg10may Sep 07 '17 at 08:43
  • `No changes detected`. My models are already in db. Though added `example` model just for checking. – garg10may Sep 07 '17 at 08:48
  • Do you import your `kyc_connect` app somewhere? If so and if that import is made *before* your models are loaded, this error also occurs. – Max M Sep 07 '17 at 09:05
  • I just found [this question](https://stackoverflow.com/questions/35388637/runtimeerror-model-class-django-contrib-sites-models-site-doesnt-declare-an-ex) where the solution was to add `'django.contrib.sites'` to your installed apps. You may give it a try. – Max M Sep 07 '17 at 09:08
  • No that didn't help either. – garg10may Sep 07 '17 at 09:16

4 Answers4

1

I had the same problem in my project and ended up resolving it by removing the settings.configure() in my conf.py file and running

make clean
make html

in my docs directory.

katyjg
  • 91
  • 4
0

I had the same error in my project. I resolved it by changing how I import the model in all my app files. For example to register the model in admin.py change

from project.app.models import YourModel

to..

from .models import Your.Model
user3278984
  • 1
  • 1
  • 1
0

For others who are still struggling with this issue, this may help:

please pay attention to have:

.. automodule:: appName.models

and not something like:

.. automodule:: ProjectName.appName.models

could append if like me you prefer, for some reasons, to have your docs directory in the parent folder of your django root directory.

Note: For working properly, my views and tasks must be set with:

.. automodule:: ProjectName.appName.tasks
.. automodule:: ProjectName.appName.views

Hope it will save time.

Gnawavibes
  • 91
  • 8
0

You can try to replace

settings.configure()

with

os.environ['DJANGO_SETTINGS_MODULE'] = 'ProjectName.settings',

then check abspath() here:

sys.path.insert(0, os.path.abspath('..'))

so it should lead to parent dir for "docs" folder,

then check index.rst

.. automodule:: appName.tasks
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29