Django:ContentType.objects.get()throws" django.db.utils.ProgrammingError:relation" django_content_type"不存在"

时间:2014-08-03 10:43:08

标签: python django django-contenttypes

在我的Django应用中,models.py中有两行使用ContentType.objects.get()

class Comment (Content):
    text = models.CharField(max_length=500)
    proposal = models.ForeignKey("Proposal", null=False, related_name="comments")
    contentType = ContentType.objects.get(app_label="myapp", model="comment")

class Proposal (Content):
    title = models.CharField(max_length=100)
    text = models.TextField()
    contentType = ContentType.objects.get(app_label="myapp", model="proposal")

当我运行python manage.py syncdb甚至python manage.py shelldjango.db.utils.ProgrammingError: relation "django_content_type" does not exist)时,他们会抛出错误。我试过删除并重新创建数据库无济于事。 如果我注释掉这两行,syncdb工作正常(但这显然打破了应用程序)。

django.contrib.contenttypes位于我的INSTALLED_APPS

以下是完整的追溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 284, in execute
    self.validate()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 310, in validate
    num_errors = get_validation_errors(s, app)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name, True)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/path/to/virtualenv/lib/python3.4/importlib/__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/path/to/django/app/models.py", line 42, in <module>
    class Comment (Content):
  File "/path/to/django/app/models.py", line 46, in Comment
    contentType = ContentType.objects.get(app_label="myapp", model="comment")
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 782, in execute_sql
    cursor.execute(sql, params)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/utils/six.py", line 535, in reraise
    raise value.with_traceback(tb)
  File "/path/to/virtualenv/lib/python3.4/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...

1 个答案:

答案 0 :(得分:1)

我不确定你要做什么,但你不能在另一个模型的定义中使用这样的模型对象。您要求Django在其执行任何其他操作之前获取ContentType的特定实例 - 甚至在它有机会为ContentType创建表之前。您需要一个ContentType的外键。

class Comment (Content):
    text = models.CharField(max_length=500)
    proposal = models.ForeignKey("Proposal", null=False, related_name="comments")
    contentType = models.ForeignKey(ContentType)

假设您实际所处的是一个通用关系,您还需要为关系所指向的任何类的对象id添加一个字段,再加上一个伪字段以便更容易访问:

    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('contentType', 'object_id')

请参阅the generic relations documentation