在我的自定义模板过滤器文件中导入models.py出现“ django.core.exceptions.AppRegistryNotReady:应用尚未加载。”

时间:2019-10-06 11:41:44

标签: python django

我想创建模板过滤器,该过滤器将获取值并在模型中对其进行搜索

import django
django.setup()

试图这样做没有帮助

这是我的代码

catalog_custom_tags.py

from catalog import models
from django import template, shortcuts

register = template.Library()

@register.filter(name='test')
def test(value):
    item = shortcuts.get_object_or_404(models.Equipment, pk=value)
    return item.name

models.py

from django.db import models


class EquipmentCategory(models.Model):
    name = models.CharField(verbose_name="Name", max_length=30, unique=True)
    category = models.CharField(verbose_name="Category", max_length=20, unique=True)

    def __str__(self):
        return self.name


class EquipmentSubCategory(models.Model):
    name = models.CharField(verbose_name="Name", max_length=30, unique=True)
    category = models.ForeignKey(EquipmentCategory, on_delete=models.CASCADE)
    subcategory = models.CharField(verbose_name="Sub Category", max_length=20, unique=True)

    def __str__(self):
        return self.name


class Material(models.Model):
    name = models.CharField(verbose_name="Name", max_length=30, unique=True)
    material = models.CharField(verbose_name="Material", max_length=30, unique=True)

    def __str__(self):
        return self.name


class Equipment(models.Model):
    name = models.CharField(verbose_name="Name", max_length=30)
    # category = models.CharField(verbose_name="Category", max_length=20)
    # subcategory = models.CharField(verbose_name="Sub Category", max_length=20)
    category = models.ForeignKey(EquipmentCategory, on_delete=models.CASCADE)
    subcategory = models.ForeignKey(EquipmentSubCategory, on_delete=models.CASCADE)
    dimension_x = models.IntegerField(verbose_name="Dimensions X(cm)", default=0)
    dimension_y = models.IntegerField(verbose_name="Dimensions Y(cm)", default=0)
    dimension_z = models.IntegerField(verbose_name="Dimensions Z(cm)", default=0)
    weight = models.FloatField(verbose_name="Weight(kg)", default=0)
    qty = models.IntegerField(verbose_name="Quantity", default=0)
    image = models.ImageField(verbose_name="Foto", blank=True, upload_to='fotos/', max_length=20)
    accessories = models.TextField(verbose_name="accessories", blank=True)
    material = models.ForeignKey(Material, on_delete=models.CASCADE)
    # material = models.CharField(verbose_name="Material", max_length=20, null=True)
    color = models.CharField(verbose_name="Color", max_length=20, null=True)

    location = models.CharField(verbose_name="Location", blank=True, max_length=20)
    description = models.TextField(verbose_name="Description", blank=True)

    def __str__(self):
        return self.name

我得到这个错误

 File "C:\Users\mro\PycharmProjects\EuroWeb\catalog\templatetags\catalog_custom_tags.py", line 1, in <module>
    from catalog import models
  File "C:\Users\mro\PycharmProjects\EuroWeb\catalog\models.py", line 4, in <module>
    class EquipmentCategory(models.Model):

应用未加载错误

File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

完整追溯

Traceback (most recent call last):
  File "C:/Users/mro/PycharmProjects/EuroWeb/manage.py", line 21, in <module>
    main()
  File "C:/Users/mro/PycharmProjects/EuroWeb/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\core\management\__init__.py", line 325, in execute
    settings.INSTALLED_APPS
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
    self._setup(name)
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\conf\__init__.py", line 66, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\conf\__init__.py", line 157, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\mro\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\mro\PycharmProjects\EuroWeb\EuroWeb\settings.py", line 140, in <module>
    django.setup()
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\mro\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\mro\PycharmProjects\EuroWeb\catalog\templatetags\catalog_custom_tags.py", line 1, in <module>
    from catalog import models
  File "C:\Users\mro\PycharmProjects\EuroWeb\catalog\models.py", line 4, in <module>
    class EquipmentCategory(models.Model):
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\db\models\base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\mro\PycharmProjects\EuroWeb\venv\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

2 个答案:

答案 0 :(得分:0)

文件结构应该是这样的。

polls/
__init__.py
models.py
templatetags/
    __init__.py
    poll_extras.py
views.py

和模板

{% load poll_extras %}

https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/#code-layout

答案 1 :(得分:0)

好的...我找到了解决方案。我很高兴有人能告诉我为什么会这样...

解决方案是: 将导致此错误的过滤器放到另一个文件中