【问题标题】:Integrating pinax-notifications failed in django app在 django 应用程序中集成 pinax 通知失败
【发布时间】:2017-05-09 17:55:43
【问题描述】:

我正在使用django-1.10 并希望使用pinax-notifications-4.0 为我的应用程序实现一些通知行为。

我正在关注quickstart 将其包含到INSTALLED_APP

INSTALLED_APPS = [
    # ...
    "pinax.notifications",
    # ...
]

然后和usage 指南。

首先是在heat/handler.py

中创建通知类型
from pinax.notifications.models import NoticeType
from django.conf import settings
from django.utils.translation import ugettext_noop as _

def create_notice_types(sender, **kwargs): 
    NoticeType.create(
        "heat_detection", 
        _("Heat Detected"), 
        _("you have detected a heat record")
    )

在应用迁移后,再次调用处理程序创建通知。 heat.apps.py

from .handlers import create_notice_types

from django.apps import AppConfig
from django.db.models.signals import post_migrate

class HeatConfig(AppConfig):
    name = 'heat'

    def ready(self):
        post_migrate.connect(create_notice_types, sender=self)

最后将 appconfig 包含到 heat.__init__.py

default_app_config = 'heat.apps.HeatConfig'

但是当尝试运行这些时:

python manage.py makemigrations pinax.notifications

我收到此错误:RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

然后我尝试将INSTALLED_APPS 中的pinax.notifications 更改为pinax-notifications。服务器给我这个错误:ImportError: No module named pinax-notifications

如何做到这一点?

【问题讨论】:

  • 您的INSTALLED_APPS 中有django.contrib.sites 吗?
  • 我添加并将pinax.notifications 更改为pinax 现在它的工作我不知道为什么但文档明确指出pinax.notifications
  • 也看到了。
  • 但在此之前我在heat.__init__.py 中评论了default_app_config = 'heat.apps.HeatConfig',当尝试取消评论并尝试运行服务器时,我遇到了错误django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
  • 您可以只使用INSTALLED_APPS 中的整个虚线路径,而不是使用default_app_config

标签: python django notifications pinax


【解决方案1】:

我可以通过更改 heat.apps.py 文件来解决它

from django.apps import AppConfig
from django.db.models.signals import post_migrate
from .handlers import create_notice_types

class HeatConfig(AppConfig):
    name = 'heat'

    def ready(self):        
        post_migrate.connect(create_notice_types, sender=self)

到这个。

from django.apps import AppConfig

class HeatConfig(AppConfig):
    name = 'heat'

    def ready(self):
        from django.db.models.signals import post_migrate
        from .handlers import create_notice_types

        post_migrate.connect(create_notice_types, sender=self)

【讨论】:

    【解决方案2】:

    作为记录,我也遇到了这个问题,并且发现,正如 Roel Delos Reyes 之前所做的那样,将应用程序名称更改为 pinax(而不是文档中非常清楚地指出的 pinax.notifications)似乎已经解决了问题。

    当我进行此更改时,makemigrations 找到了所有迁移。

    我实际上同时使用了“pinax.notifications”和“pinax.templates”(正如通知文档所建议的那样),并且我看到两套 文档都清楚地指定了pinax.<something>。我无法解释......文档怎么可能是那个错误的?两次?

    (由于其他不相关的原因,我使用 Django 1.19 而不是 2.0,但我认为这并不重要。)

    无论如何——“这行得通。” HTH.™

    重要编辑:我随后发现INSTALLED_APPS 中需要两个 pinax pinax.notifications。如果没有后者,migrate 将不会应用所有迁移。

    INSTALLED_APPS = [
       ...
       'pinax',
       'pinax.notifications', 
       ...
       ]
    

    我还在 GitHub 上的项目中打开了(并且已经关闭)一个故障单,因此请同时参考该站点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2015-04-07
      • 2020-01-19
      相关资源
      最近更新 更多