Django-Sphinx RuntimeError: Model class models.Project doesn't declare

716 Views Asked by At

I am attempting to generate a html documentation with Sphinx of a django project. I am getting the following error when executing make html on windows cmd. My settings.py contains an entry for the application I built, where the models.py file belongs to. For confidentially reasons, I call it here project_name.

INSTALLED_APPS = [
    'project_name.apps.project_nameAppConfig',
    'django.contrib.admin',
    'django.contrib.sites',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'import_export',
    'nested_admin',
    'report_builder',
    'rest_framework',
    'ajax_select',
]

Error Trace:

> C:\django_project\docs\source\models.rst:4: WARNING: autodoc: failed to
> import module 'models'; the following exception was raised: Traceback
> (most recent call last):   File
> "C:\Users\...\AppData\Local\Continuum\Anaconda3\lib\site-packages\sphinx\ext\autodoc.py",
> line 658, in import_object
>     __import__(self.modname)   File "C:\django_project\project_name\models.py", line 8, in <module>
>     class Project(models.Model):   File "C:\Users\...\AppData\Local\Continuum\Anaconda3\lib\site-packages\django\db\models\base.py",
> line 118, in __new__
>     "INSTALLED_APPS." % (module, name) RuntimeError: Model class models.Project doesn't declare an explicit app_label and isn't in an
> application in INSTALLED_APPS.

Thanks for your help!

1

There are 1 best solutions below

0
On

What solved the issue for me is creating an abstract class and inheriting the other model classes from there instead of directly from the django.db models class. In code something along this lines:

from django.db import models
class BaseModel(models.Model):

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


class Project(BaseModel):
    name = models.CharField(max_length=200, unique=True)
    description = models.TextField
    client = models.CharField(max_length=200)

    def __str__(self):
        return self.name

...

Got the idea from here: Model class doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS