模型类未声明显式的app_label,也不在INSTALLED_APPS中的应用程序中 [英] Model class doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

查看:74
本文介绍了模型类未声明显式的app_label,也不在INSTALLED_APPS中的应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sphinx,并试图为我的Django项目生成文档.我决定先尝试记录模型,以便在我的.rst文件中做到这一点

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:

但是出现以下错误

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.

经过Google搜索后,我在这里找到了解决方法 Django 1.9弃用警告app_label 参见РоманАрсеньев对"2016年1月11日14:54"的评论

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"

我进入了我的models.py并在我在models.py中拥有的每个单独的类中添加了上面的"class Meta:app_label".这样就解决了问题,我可以使用sphinx-build脚本为我的模型生成文档.

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.

现在我想对views.py做同样的事情,并在我的.rst文件中做到了

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是Django应用,在我的INSTALLED_APPS元组中的设置文件中列出了它.sphinx docs文件夹与wdland文件夹处于同一级别.

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.

在该主题上阅读相似的主题似乎指向的是被调用时尚未加载的模块.我不知道如何修复狮身人面像和Django中没有的经验.关于如何解决此问题的任何想法吗?

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?

尝试记录urls.py会产生类似的错误.

Trying to document urls.py would yield similar errors.

这是我的观点的开始.

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

推荐答案

我的解决方案是在应用程序中的 models.py 上创建一个 BaseModel 抽象类.您的错误可能是:

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:

模型类 wdland.models.Device 没有声明显式的 app_label ,并且不在INSTALLED_APPS中的应用程序中.

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

我在其他应用程序上也遇到了相同的错误.添加请求的"app_label"即可解决该问题.

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

这篇关于模型类未声明显式的app_label,也不在INSTALLED_APPS中的应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆