断言错误:Django-rest-Framework

Dan*_*Man 18 python django postgresql backbone.js django-rest-framework

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

我一直在研究"轻量级Django - Elman和Lavin"这本书,我在第4章和第5章中已经被困了几天,我们应该使用其余的框架和backbone.js.例如,见

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

几天前,我试图通过本书中提供的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: 
Run Code Online (Sandbox Code Playgroud)

关系字段必须提供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']
Run Code Online (Sandbox Code Playgroud)

这个错误出现在"relations.py"中,它属于django-rest-framework.因为我使用的是上面链接中提供的确切代码,所以它应该没有错误.实际上,我改变了唯一的一段代码在settings.py中(重复发生错误后):

之前:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scrum',
    }
}
Run Code Online (Sandbox Code Playgroud)

现在:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scrum',
        'USER': 'daniel', 
        'PASSWORD': '12345',
        'HOST': '127.0.0.1',
        'PORT': '5432',        
    }
Run Code Online (Sandbox Code Playgroud)

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

Role name |                   Attributes                   | Member of | Description 
-----------+------------------------------------------------+-----------+-------------
 daniel    | Superuser, Create DB                           | {}        | 
 postgres  | Superuser, Create role, Create DB, Replication | {}        | 
Run Code Online (Sandbox Code Playgroud)

最后,似乎我对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
Run Code Online (Sandbox Code Playgroud)

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

Ald*_*und 40

这里阅读DRF文档.

在版本2.xa中,如果正在使用ModelSerializer类,则序列化程序类有时可以自动确定queryset参数.

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

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

serializers.py中 有这一行:

assigned = serializers.SlugRelatedField(slug_field=User.USERNAME_FIELD, required=False)
Run Code Online (Sandbox Code Playgroud)

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

  • 有一天,django 会正常工作并且不会出现无意义的错误,或者有一天我会将这个损坏的框架中的所有内容移至 Flask 中。 (2认同)