postgresql 断言错误:Django-rest-Framework

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27484344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:42:54  来源:igfitidea点击:

Assertion error at: Django-rest-Framework

pythondjangopostgresqlbackbone.jsdjango-rest-framework

提问by DanielTheRocketMan

I am using python 3.4, Django 1.7.1 (the version considered in the book), Postgres 9.3 and my IDE is Eclipse.

我使用的是 python 3.4、Django 1.7.1(书中考虑的版本)、Postgres 9.3,我的 IDE 是 Eclipse。

I have been studying the book "Lightweight Django - Elman and Lavin" and I have been stuck for days in the chapters 4 and 5, where we are supposed to use the rest framework and backbone.js. See for instance,

我一直在学习“轻量级 Django - Elman 和 Lavin”这本书,我在第 4 章和第 5 章被困了好几天,我们应该在那里使用其余框架和backbone.js。参见例如,

Lightweight Django - Chapters 4 and 5

轻量级 Django - 第 4 章和第 5 章

Some days ago, I tried to code by myseld as presented in the book and also checking with the examples presented in the link above. However, since I was not going ahead, I decided to copy the code presented in the link above and tried to run. The same error has arisen:

几天前,我尝试按照书中介绍的方式通过 myseld 进行编码,并检查上面链接中提供的示例。但是,由于我没有继续,我决定复制上面链接中提供的代码并尝试运行。出现了同样的错误:

AssertionError at /

Relational field must provide a `queryset` argument, or set read_only=`True`.

Request Method:     GET
Request URL:    http://127.0.0.1:8000/
Django Version:     1.7.1
Exception Type:     AssertionError
Exception Value: 

Relational field must provide a querysetargument, or set read_only=True.

关系字段必须提供queryset参数,或设置 read_only= True

Exception Location:     /usr/local/lib/python3.4/dist-packages/rest_framework/relations.py in __init__, line 35
Python Executable:  /usr/bin/python3
Python Version:     3.4.0
Python Path:    

['/home/daniel/workspace/Scrum',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-i386-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages']

This error arises inside "relations.py, which belongs to the django-rest-framework. Since I am using the exact code presented in the link above, it is supposed to not have errors. Actually, the only piece of code that I changed was in the settings.py (after the error repeatedly happened):

这个错误出现在“relations.py,它属于 django-rest-framework。由于我使用的是上面链接中提供的确切代码,它应该没有错误。实际上,我更改的唯一一段代码在settings.py中(在错误反复发生之后):

Before:

前:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scrum',
    }
}

Now:

现在:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scrum',
        'USER': 'daniel', 
        'PASSWORD': '12345',
        'HOST': '127.0.0.1',
        'PORT': '5432',        
    }

As you can see below, my user "daniel" has the following attributes:

如下所示,我的用户“daniel”具有以下属性:

Role name |                   Attributes                   | Member of | Description 
-----------+------------------------------------------------+-----------+-------------
 daniel    | Superuser, Create DB                           | {}        | 
 postgres  | Superuser, Create role, Create DB, Replication | {}        | 

Finally, it seems that I do not have any problem with the installation of psycopg2, since I was able to create "scrum" as presented below. In fact, the list of databases of my system is

最后,我似乎对 psycopg2 的安装没有任何问题,因为我能够创建如下所示的“scrum”。事实上,我的系统的数据库列表是

                                      List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 scrum     | daniel   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/daniel           +
           |          |          |             |             | daniel=CTc/daniel
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres

Can someone help me to discover the problem?

有人可以帮我发现问题吗?

回答by Aldarund

Read the DRF docs here.

此处阅读 DRF 文档。

In version 2.x a serializer class could sometimes automatically determine the queryset argument if a ModelSerializer class was being used.

This behavior is now replaced with always using an explicit queryset argument for writable relational fields.

在版本 2.xa 中,如果使用 ModelSerializer 类,序列化器类有时可以自动确定查询集参数。

此行为现​​在被替换为始终对可写关系字段使用显式查询集参数。

You are just using a newer version of DRF than the authors of the code used, so you'll need to either use a lower version or fix the code.

您只是使用比所用代码作者更新的 DRF 版本,因此您需要使用较低版本或修复代码。

In serializers.pythere's this line:

serializers.py 中有这一行:

assigned = serializers.SlugRelatedField(slug_field=User.USERNAME_FIELD, required=False)

You need to either add read_only=Trueor queryset=User.objects.all()

您需要添加read_only=Truequeryset=User.objects.all()