update_all_contenttypes seemingly not working with Django 1.8
Asked Answered
T

0

4

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://mcmap.net/q/355928/-importerror-cannot-import-name-update_all_contenttypes ).

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.
Tonguelash answered 21/9, 2015 at 16:54 Comment(3)
Please show the full traceback. If you change the real code to SomeObject.objects then it might be much more difficult to spot the problem.Dollhouse
Did so. It's proprietary code so I had to change it.Tonguelash
I don't suppose this is an issue to you anymore, considering how old the question is. I tried to do the same thing as you and for me it worked fine. I believe your issue had something to do with unclean data, unapplied migrations or something along those lines.Benavidez

© 2022 - 2024 — McMap. All rights reserved.