问题
I was getting the following error upon running a certain function:
django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.
Based upon ContentType matching query does not exist on post_syncdb I tried doing
from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes() # make sure all content types exist
at the beginning of that function.
Of course, as ImportError: cannot import name update_all_contenttypes mentions, update_all_contenttypes appears to have been silently removed in Django 1.8 , so that didn't work. Next I tried removing the above snippet and instead adding the following to the beginning of that function:
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()
(Other than the last line, i.e. update_all_contenttypes(), the snippet directly above this comes from https://stackoverflow.com/a/29550255/805141 ).
However, I'm still getting django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.
The line I'm getting that error on is SomeObject.objects.all().delete()
Here's the full stack trace:
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.
来源:https://stackoverflow.com/questions/32700638/update-all-contenttypes-seemingly-not-working-with-django-1-8