Skip to main content
 首页 » 编程设计

python之Django:ContentType.objects.get() 抛出 "django.db.utils.ProgrammingError: relation "django_content_type“不存在”

2025年05月04日31kevingrace

在我的 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 shell 时,它们会抛出一个错误 (django.db.utils.ProgrammingError: relation "django_content_type"不存在)。我试过删除并重新创建数据库无济于事。 如果我注释掉这两行,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... 

请您参考如下方法:

我不确定您要做什么,但是您不能像在另一个模型的定义中那样使用模型对象。您要求 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 .