F
F
Finger Gross2016-09-14 00:41:43
Django
Finger Gross, 2016-09-14 00:41:43

How to use ContentType in Django 1.10?

Hello
Tell me what is the problem and how to access ContentType ? The problem started with the move to Django 1.10. On 1.8 everything was fine...
Stack trace

ImportError: Failed to import test module: api.tests.test_cart_item
Traceback (most recent call last):
  File "/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "/api/tests/test_cart_item.py", line 8, in <module>
    from common.base_test import BaseTest
  File "/common/base_test.py", line 14, in <module>
    from ..cart.models import Order
  File "/cart/models.py", line 8, in <module>
    from django.contrib.auth.models import User
  File "/python2.7/site-packages/django/contrib/auth/models.py", line 6, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/python2.7/site-packages/django/contrib/contenttypes/models.py", line 138, in <module>
    class ContentType(models.Model):
  File "/python2.7/site-packages/django/db/models/base.py", line 113, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    
    # ...
    'rest_framework',
    
    'api',
    'common',
    # ...
]

base_test.py
from ..cart.models import Order
from ..cart.models import Item
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType


class BaseTest(TestCase):
    csrf_checks = False
    
    # ...

cart/models.py
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType


class Order(models.Model):
    user = models.ForeignKey(User)
    # ...

    class Meta:
        app_label = 'cart'


class Item(models.Model):
    order = models.ForeignKey(Order)
    # ...
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()

    class Meta:
        app_label = 'cart'

cart/apps.py
from django.apps import AppConfig

class CartConfig(AppConfig):
    name = 'cart'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Klimenko, 2016-09-14
@aklim007

do you have the cart module in INSTALLED_APPS?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question