断言错误:Django-rest-Framework

问题描述 投票:18回答:1

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

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

Lightweight Django - Chapters 4 and 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: 

关系字段必须提供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']

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

之前:

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

现在:

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

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

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

最后,似乎我对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

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

python django postgresql backbone.js django-rest-framework
1个回答
37
投票

阅读DRF docs here

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

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

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

serializers.py有这条线:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.