In my REST-like API, I have a mechanism to allow users to customize models (add custom fields and add custom models).
This relies on a set of models describing the customizations.
Here is a simplified version of my code to show the concept.
+ myproject
+ customization
. models.py
class CustomModel(django.db.models.Model):
# […]
class CustomField(django.db.models.Model):
# […]
and
+ 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
# […]
In Django 1.6, I have no problem making everything work.
Now, transitionning to Django 1.11, I've had some cleanup to do, mainly because it's impossible to import a model inside the root package of an application. (see also this answer).
I've eleminated every AppRegistryNotReady
exception that I had, but for the one in myproject.entity.models
module. Indeed, to build my Entities, I have to query to the CustomField
and CustomModel
tables, but as I'm currently building my models, Django forbids me to do so.
I am stuck in this kind of circular dependency. Any thought towards a workaround are welcome.
EDIT: Stack trace follows
[…]
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.