GrossFinger
@GrossFinger

Как использовать ContentType в Django 1.10?

Привет

Скажите, в чем проблема и как обращаться к ContentType? Проблема началась с переходом на Django 1.10. На 1.8 все нормально было...

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'
  • Вопрос задан
  • 898 просмотров
Пригласить эксперта
Ответы на вопрос 1
модуль cart в INSTALLED_APPS у вас есть?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы