3

我正在尝试将一些数据从 django 项目上的转储数据生成的 json 文件加载到使用 loaddata 的新数据库的新文件中。与 db 的连接似乎有效,但几乎马上我就收到了拒绝关系 django_content_type 的权限。我没有得到我们所说的许可。db 是单独服务器上的 postgres,我通过笔记本电脑上的 vagrant 虚拟机连接到它,该虚拟机具有虚拟环境,并且 settings.py 具有连接到 db 的设置。这是回溯:

Problem installing fixture 'djgprd1_dumpdata.json': Traceback (most recent call last):
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 196, in handle
    obj.save(using=using)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/base.py", line 524, in save_base
    manager.using(using).filter(pk=pk_val).exists())):
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 565, in exists
    return self.query.has_results(using=self.db)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 441, in has_results
    return bool(compiler.execute_sql(SINGLE))
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    cursor.execute(sql, params)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/home/vagrant/devenv/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
    return self.cursor.execute(query, args)
DatabaseError: Could not load contenttypes.ContentType(pk=29): permission denied for relation django_content_type

我在这里做错了什么?

4

2 回答 2

4

这个数据库的 postgres 用户是自动创建的,我没有检查权限,这个数据库缺少它们,所以GRANT ALL PRIVILEGES ON DATABASE x for user y;解决了权限问题。我的错。

于 2013-10-07T16:09:41.403 回答
2

看起来您正在导出ContentType对象(您是否dumpdata使用过--all?),这是设计使然吗?

文档提到您可能想要使用该--natural标志来解决此类问题,请参阅https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---natural

另请参阅此报价:

如果您从实现通用关系的模型序列化数据(例如,在生成固定装置时),您可能应该使用自然键来唯一标识相关的 ContentType 对象。有关更多信息,请参阅自然键和转储数据 --natural。

来源:https ://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

于 2013-10-07T10:32:18.447 回答