django filtering querysets causes 'AppRegistryNotReady: Models aren't loaded yet.' with forms within models.py -
during development of our current django-project (django 1.7.1) reach task mark database-entries deleted , remove them user's sight not db.
a fast straightforward solution found django-logicaldelete (exactly want do!)
following instruction django-logicaldelete installed logicaldelete pip , added installed_app settings.py
only thing left add logicaldelete in models.py , admin.py inherit it
... import logicaldelete class mymodel(logicaldelete.models.model){...} ...
... import logicaldelete class mymodeladmin(logicaldelete.admin.modeladmin){...} ...
so far :) trying run project causes 'appregistrynotready: models aren't loaded yet.' - error:
traceback (most recent call last): file "c:\python34\lib\site-packages\django\db\models\options.py", line 414, in get_field_by_name return self._name_map[name] attributeerror: 'options' object has no attribute '_name_map' during handling of above exception, exception occurred: traceback (most recent call last): file "c:\python34\lib\site-packages\django\db\models\options.py", line 561, in get_all_related_m2m_objects_with_model cache = self._related_many_to_many_cache attributeerror: 'options' object has no attribute '_related_many_to_many_cache' during handling of above exception, exception occurred: traceback (most recent call last): file "c:\eclipse\workspacepraxis\promotionsdatenbank\manage.py", line 10, in <module> execute_from_command_line(sys.argv) file "c:\python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line utility.execute() file "c:\python34\lib\site-packages\django\core\management\__init__.py", line 354, in execute django.setup() file "c:\python34\lib\site-packages\django\__init__.py", line 21, in setup apps.populate(settings.installed_apps) file "c:\python34\lib\site-packages\django\apps\registry.py", line 108, in populate app_config.import_models(all_models) file "c:\python34\lib\site-packages\django\apps\config.py", line 202, in import_models self.models_module = import_module(models_module_name) file "c:\python34\lib\importlib\__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) file "<frozen importlib._bootstrap>", line 2254, in _gcd_import file "<frozen importlib._bootstrap>", line 2237, in _find_and_load file "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked file "<frozen importlib._bootstrap>", line 1200, in _load_unlocked file "<frozen importlib._bootstrap>", line 1129, in _exec file "<frozen importlib._bootstrap>", line 1471, in exec_module file "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed file "c:\eclipse\workspacepraxis\promotionsdatenbank\personen\models.py", line 144, in <module> class promovendchoice(forms.form): file "c:\eclipse\workspacepraxis\promotionsdatenbank\personen\models.py", line 149, in promovendchoice promovend_choice = forms.modelchoicefield(queryset=promovend.objects.all(), to_field_name='nachname', empty_label="promovend") file "c:\python34\lib\site-packages\django\db\models\manager.py", line 191, in return self.get_queryset() file "c:\python34\lib\site-packages\logicaldelete\managers.py", line 15, in get_queryset return logicaldeletequeryset(self.model, using=self._db).filter(date_removed__isnull=true) file "c:\python34\lib\site-packages\django\db\models\query.py", line 691, in filter return self._filter_or_exclude(false, *args, **kwargs) file "c:\python34\lib\site-packages\django\db\models\query.py", line 709, in _filter_or_exclude clone.query.add_q(q(*args, **kwargs)) file "c:\python34\lib\site-packages\django\db\models\sql\query.py", line 1287, in add_q clause, require_inner = self._add_q(where_part, self.used_aliases) file "c:\python34\lib\site-packages\django\db\models\sql\query.py", line 1314, in _add_q current_negated=current_negated, connector=connector) file "c:\python34\lib\site-packages\django\db\models\sql\query.py", line 1138, in build_filter lookups, parts, reffed_aggregate = self.solve_lookup_type(arg) file "c:\python34\lib\site-packages\django\db\models\sql\query.py", line 1076, in solve_lookup_type _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) file "c:\python34\lib\site-packages\django\db\models\sql\query.py", line 1339, in names_to_path field, model, direct, m2m = opts.get_field_by_name(name) file "c:\python34\lib\site-packages\django\db\models\options.py", line 416, in get_field_by_name cache = self.init_name_map() file "c:\python34\lib\site-packages\django\db\models\options.py", line 445, in init_name_map f, model in self.get_all_related_m2m_objects_with_model(): file "c:\python34\lib\site-packages\django\db\models\options.py", line 563, in get_all_related_m2m_objects_with_model cache = self._fill_related_many_to_many_cache() file "c:\python34\lib\site-packages\django\db\models\options.py", line 577, in _fill_related_many_to_many_cache klass in self.apps.get_models(): file "c:\python34\lib\functools.py", line 428, in wrapper result = user_function(*args, **kwds) file "c:\python34\lib\site-packages\django\apps\registry.py", line 168, in get_models self.check_models_ready() file "c:\python34\lib\site-packages\django\apps\registry.py", line 131, in check_models_ready raise appregistrynotready("models aren't loaded yet.") django.core.exceptions.appregistrynotready: models aren't loaded yet.
i read problem since django 1.7 , solution
import django django.setup()
is given. here doesn't work add lines. tried add in logicaldelete.managers.py , logicaldelete.query.py
seems i've got wrong idea causes problem.
the part of own code shown in errors' traceback ist call within form:
promovend_choice = forms.modelchoicefield(queryset=promovend.objects.all(), to_field_name='nachname', empty_label="promovend")
hope know way solve this...
if not i'll override delete-method , add manager on own.
update no. 3
i've implemented basemodel , suitable manager on own (it hardly differs logicaldelete)
class softdeletebasemodel(models.model): deleted = models.booleanfield(default=false) objects = softdeletemanager(false) objects_deleted = softdeletemanager(true) all_objects = models.manager() def delete(self): related_models = [relation.get_accessor_name() relation in self._meta.get_all_related_objects()] model_objects in rleated_models: related_objects = getattr(self, model_objects).all() related_object in related_objects: if not issubclass(related_object.__class__, softdeletebasemodel): break related_object.delete() self.deleted= true self.save() class meta: abstract = true
class softdeletemanager(models.manager): def __init__(self, show_deleted=false): super(softdeletemanager, self).__init__() self.show_deleted = show_deleted def get_queryset(self): qs = super(softdeletemanager, self).get_queryset() return qs.filter(deleted=self.show_deleted)
unfortunately same error occurs. importing django calling django.setup() in managers.py file doesn't work either.
there problem calling (self-made) manager with
objects = softdeletemanager(false) objects_deleted = softdeletemanager(true) all_objects = models.manager()
seems softdeletemanger isn't ready (loaded) while model wants refer it..
update no. 4
new findings:
suggesting error lies within form wasn't bad! wondered using queryset = model.objects.all()
standard-manager works fine, replacing softdeletemanager causes error...
the idea came up: why not adding filter codeline using standard-manager? queryset = models.objects.all().filter(deleted=false)
code causes appregistrynotready-error - root cause seems filtering querysets within self-made manager...
edit
i found similiar problem filtering querysets here. mentioned @ top tried using import django
, django.setup()
without success...
update no. 5
after reading hundred times calling django.setup()
solve problem called within models.py, expecting error or successful run... nothing happens @ all. running project creates python-process there no output neither development server starts...
greetings, tobi
finally found "hidden" solution in previous post somewhere (i unfortunately lost source):
there django.form used in models.py. supposing forms' command causes error re-structured app , created seperate forms.py file form. works perfeclty!
Comments
Post a Comment