update_all_contenttypes似乎无法使用Django 1.8

时间:2015-09-21 16:54:18

标签: django django-models runtime-error django-1.8 django-contenttypes

运行某个功能时出现以下错误:

django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.

基于ContentType matching query does not exist on post_syncdb我尝试过

from django.contrib.contenttypes.management import update_all_contenttypes
 update_all_contenttypes() # make sure all content types exist

在该函数的开头。

当然,正如ImportError: cannot import name update_all_contenttypes提到的那样,update_all_contenttypes似乎已在Django 1.8中被静默删除,因此无法正常工作。接下来,我尝试删除上面的代码段,而不是将以下内容添加到该函数的开头:

from django.apps import apps
from django.contrib.contenttypes.management import update_contenttypes

def update_all_contenttypes(**kwargs):
    for app_config in apps.get_app_configs():
        update_contenttypes(app_config, **kwargs)

update_all_contenttypes()

(除了最后一行,即update_all_contenttypes(),直接在其上方的代码段来自https://stackoverflow.com/a/29550255/805141)。

但是,我仍然得到django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.

我遇到错误的行是SomeObject.objects.all().delete()

这里是完整的堆栈跟踪:

some_function() 
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/path/to/app/models.py", line XXXX, in some_function
    SomeObject.objects.all().delete()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 536, in delete
    collector.collect(del_query)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/deletion.py", line 197, in collect
    reverse_dependency=reverse_dependency)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/deletion.py", line 97, in add
    if not objs:
  File /path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 166, in __bool__
    self._fetch_all()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/path/to/virtualenv/lib/python3.4/site-packages/polymorphic/query.py", line 296, in iterator
    real_results = self._get_real_instances(base_result_objects)
  File "/path/to/virtualenv/lib/python3.4/site-packages/polymorphic/query.py", line 199, in _get_real_instances
    real_concrete_class = base_object.get_real_instance_class()
  File "/path/to/virtualenv/lib/python3.4/site-packages/polymorphic/polymorphic_model.py", line 105, in get_real_instance_class
    model = ContentType.objects.get_for_id(self.polymorphic_ctype_id).model_class()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 136, in get_for_id
    ct = self.get(pk=id)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 334, in get
    self.model._meta.object_name
django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.

0 个答案:

没有答案