11

I am using sphinx and was trying to generate documentation for my Django project. I decided to first try to document the models so in my .rst file I did this

wdland\.models
==============

.. automodule:: wdland.models
    :members:
    :undoc-members:
    :show-inheritance:

But the get the following errors

WARNING: /home/fabou/wdlandenvpy3/source/docs/wdland.rst:9: (WARNING/2) autodoc: failed to import module 'wdland.models'; the following exception was raised:
Traceback (most recent call last):
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 657, in import_object
    __import__(self.modname)
  File "/home/fabou/wdlandenvpy3/source/wdland/models.py", line 35, in <module>
    class Device(models.Model):
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/db/models/base.py", line 118, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class wdland.models.Device doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

After some google search I found a solution here Django 1.9 deprecation warnings app_label see comment from Роман Арсеньев on “Jan 11 '16 at 14:54”

I went in my models.py and added “class Meta: app_label” as per above post to every single class I had in models.py. This fixed the problem and I was able to generate the documentation for my models using the sphinx-build script.

Now I wanted to do the same for views.py and did this in my .rst file

wdland\.views
=============

.. automodule:: wdland.views
    :members:
    :undoc-members:
    :show-inheritance:

But on compile I get this new similar error and I have been unable to find a solution for it nor did understand the previous fix for models.

WARNING: /home/fabou/wdlandenvpy3/source/docs/wdland.rst:19: (WARNING/2) autodoc: failed to import module 'wdland.views'; the following exception was raised:
Traceback (most recent call last):
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 657, in import_object
    __import__(self.modname)
  File "/home/fabou/wdlandenvpy3/source/wdland/views.py", line 2, in <module>
    from . import views
  File "/home/fabou/wdlandenvpy3/source/wdland/views.py", line 19, in <module>
    from .forms import TicketForm, TicketAmendForm
  File "/home/fabou/wdlandenvpy3/source/wdland/forms.py", line 1, in <module>
    from django.contrib.auth.forms import AuthenticationForm
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/contrib/auth/forms.py", line 12, in <module>
    from django.contrib.auth.models import User
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/contrib/auth/models.py", line 6, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 139, in <module>
    class ContentType(models.Model):
  File "/home/fabou/wdlandenvpy3/lib/python3.5/site-packages/django/db/models/base.py", line 118, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Here the relevant sphinx configuration

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import django
import os
import sphinx_rtd_theme
import sys

from django.conf import settings

sys.path.insert(0, os.path.abspath('../'))
settings.configure()
django.setup()

wdland is a Django app an is listed in my INSTALLED_APPS tuple in the settings file. The sphinx docs folder is at the same level as the wdland folder.

Reading similar threads on the topic seem to point to modules that are not yet loaded when being called. I have no clue how to fix as not experienced in either sphinx and Django. Any idea on how I can fix this?

Trying to document urls.py would yield similar errors.

EDIT: Here the begining of my view.py

import datetime

from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import Http404, HttpResponse
from django.template.loader import render_to_string
from django.shortcuts import render, HttpResponseRedirect
from django.views import generic
from random import randint
from script.serversnmp import get_ubuntu_snmp, get_esxi_snmp
from script.wdlandtools import get_monthly_sess_hour_stats,\
                                get_monthly_sess_nb_stats,\
                                get_monthly_ticket_stats,\
                                get_ticket_category_stats,\
                                clean_usersession_tbl, update_usertotals_tbl,\
                                send_email_notification, get_adsl_usage
from wdland.models import Device, UserSession, SupportTicket, News, UserTotals,\
                            SavedUsrSess
from .forms import TicketForm, TicketAmendForm

from jchart import Chart
from jchart.config import Axes, DataSet, rgba
Fabou78
  • 169
  • 1
  • 2
  • 12
  • Post INSTALLED_APPS list here – Neeraj Kumar Jul 23 '17 at 19:24
  • `INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'wdland', 'jchart', ]` @NeErAjKuMaR – Fabou78 Jul 24 '17 at 16:05

3 Answers3

14

My solution was to create a BaseModel abstract class on your models.py within your app, on your error this may be:

class BaseModel(models.Model):

    class Meta:
        abstract = True  # specify this model as an Abstract Model
        app_label = 'wdland'

And made all models inherit from this instead of the usual models.Model.

To solve it, I went to the basics of trying to figure out the error message, which states:

Model class wdland.models.Device doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

I also had the same error with a different app. Adding the requested "app_label" solved the issue.

alecor Dev
  • 354
  • 4
  • 6
  • 1
    This solution didn't work for me, however manually adding `class Meta: app_label = ''` to each model individually did for some reason – arshbot Jan 18 '19 at 05:50
0

Just adding "app name" to the installed apps in the settings file. In my case the "app name" is proteins and my installed apps look like this

INSTALLED_APPS = [
'rest_framework',
'proteins',
# 'proteins.apps.ProteinsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

]

olawalejuwonm
  • 1,315
  • 11
  • 17
-3

I think you don't have django.contrib.contenttypes this in INSTALLED_APPS of django settings.So add it in INSTALLED_APPS of django settings

Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41
  • you mentioned in line 2 from . import views in views.py . what is this? – Neeraj Kumar Jul 24 '17 at 16:23
  • Well I don't really know where this is coming from (I didn't notice it) as you can see on my EDIT (views.py) I have nothing in line 2. Line 19 does exist on the other hand. – Fabou78 Jul 24 '17 at 21:04
  • check in your traceback that line 2 code is there. from . import views code is there – Neeraj Kumar Jul 25 '17 at 04:11
  • I don't have that line in my view.py see my edit above, there is nothing in line 2 of my views.py it is a blank line so I don't where this error is coming from but it certainly can't be from my views.py – Fabou78 Jul 25 '17 at 21:45
  • This is not actually the case, the traceback code in view.py is there for reference. – A.J. Mar 02 '20 at 12:22