0

每当我尝试EmbeddedField使用 Djongo 在 Django 中创建一个时,我都会收到以下错误消息。我尝试了网络上建议的所有内容。但我完全被困在这里,需要帮助来克服这个问题。只要我尝试migrate或尝试runserver它就会引发此错误。我一使用EmbeddedField. 我没有修改 manage.py 文件。

错误信息

djongo django.core.expceptions.AppRegistryNotReady: Models aren't loaded yet.

模型.py

from djongo import models 

class ClientRepresentation(models.Model):
    client_main_contact = models.CharField(max_length=100)
    client_stand_in_contact = models.CharField(max_length=100)

    class Meta:
        abstract = True


class User(models.Model):
    client_name = models.CharField(max_length=100)
    client_location = models.CharField(max_length=100)

    client_representation = models.EmbeddedField(
        model_container=ClientRepresentation
        # null=True
        )

    objects = models.DjongoManager()

    def __str__(self):
        return self.client_name

设置.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # REST Framework API
    'rest_framework',

    # Own Apps
    'usr'
]

管理.py

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djongotest.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()
4

1 回答 1

1

这是 Djongo 版本 1.3.2 中产生的问题。安装 1.3.1 或更低版本不会出现此问题。

您可以通过执行检查:pip install djongo<1.3.2并运行python manage.py check以查看是否发生错误。

由于 Djongo 1.3.2 添加了一种新的验证方法来验证模型容器,因此引入了该错误,但是在那个时间点无法完成。该问题可能已在上游修复,因为该问题是在构建反向关系树时引起的,只有在所有反向模型都准备好后才能完成。

因为它只是一个验证,你可以继承 EmbeddedField 而只是不调用验证:

from djongo import models


class EmbeddedField(models.EmbeddedField):
    def __init__(self, model_container, model_form_class=None, model_form_kwargs = None, *args, **kwargs):
        self.model_container = model_container
        self.model_form_class = model_form_class
        self.model_form_kwargs = {} if model_form_kwargs is None else model_form_kwargs

        # Bypass call to _validate_container()
        super(models.fields.FormlessField, self).__init__(*args, **kwargs)


class User(models.Model):
    ...
     client_representation = EmbeddedField(
        model_container=ClientRepresentation
     )
于 2020-06-30T11:29:35.820 回答