在我类似REST的API中,我有一种机制允许用户自定义模型(添加自定义字段并添加自定义模型)。
这依赖于一组描述自定义的模型。
以下是我的代码的简化版本,以显示概念。
+ myproject
+ customization
. models.py
class CustomModel(django.db.models.Model):
# […]
class CustomField(django.db.models.Model):
# […]
和
+ myproject
+ entities
. models.py
class RootEntityModel(django.db.models.Model):
name = CharField(…)
# […]
from myproject import customization
from myproject.customization.models import CustomField
# Here apply the custom filters on the root entity type
for custom_field in CustomField.objects.filter(entity__name='RootEntityModel').all():
customize.build_field_and_apply(custom_field, RootEntityModel)
# Here instantiate custom models, etc
# […]
在Django 1.6中,我没有任何问题让一切正常。
现在,转换到Django 1.11,我做了一些清理工作,主要是因为在应用程序的根软件包中导入模型是不可能的。 (另见this answer)。
我已经解除了我所拥有的每个AppRegistryNotReady
例外情况,但对于myproject.entity.models
模块中的例外情况。实际上,要构建我的实体,我必须查询CustomField
和CustomModel
表,但正如我目前正在构建我的模型一样,Django禁止我这样做。
我陷入了这种循环依赖。我们欢迎任何对变通方法的想法。
编辑:跟踪堆栈跟踪
[…]
File "D:/Code/django-1.11/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:/Code/django-1.11/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "D:/Code/django-1.11/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "C:/Python27/lib/importlib/__init__.py", line 37, in import_module
__import__(name)
File "D:/Code/resterserver/myproject/entities/models/__init__.py", line 369, in <module>
for custom_field in CustomField.objects.filter(entity__name='RootEntityModel').all():
File "D:/Code/django-1.11/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "D:/Code/django-1.11/django/db/models/query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "D:/Code/django-1.11/django/db/models/query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "D:/Code/django-1.11/django/db/models/sql/query.py", line 1250, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "D:/Code/django-1.11/django/db/models/sql/query.py", line 1276, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "D:/Code/django-1.11/django/db/models/sql/query.py", line 1154, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "D:/Code/django-1.11/django/db/models/sql/query.py", line 1034, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "D:/Code/django-1.11/django/db/models/sql/query.py", line 1331, in names_to_path
if field.is_relation and not field.related_model:
File "D:/Code/django-1.11/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:/Code/django-1.11/django/db/models/fields/related.py", line 115, in related_model
apps.check_models_ready()
File "D:/Code/django-1.11/django/apps/registry.py", line 132, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
答案 0 :(得分:0)
我不是直接在我的models.py
模块中添加自定义模型,而是通过覆盖其AppConfig
方法在ready()
对象中执行此操作。
这是工作代码存根:
+ myproject
+ entities
. apps.py
from django.apps.config import AppConfig
class EntitiesAppConfig(AppConfig):
name = 'myproject.entities'
label = 'entities'
verbose_name = 'MyProject Entities app'
def ready(self):
super(EntitiesAppConfig, self).ready()
# HERE i can do my customizations
# ...