在我的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)。
除了myproject.entity.models模块中的异常之外,我已经排除了所有的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.发布于 2018-11-22 12:38:20
我不是直接在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
# ...https://stackoverflow.com/questions/50019420
复制相似问题