首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >断言错误: Django-rest-Framework

断言错误: Django-rest-Framework
EN

Stack Overflow用户
提问于 2014-12-15 12:40:50
回答 3查看 35.5K关注 0票数 23

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

我一直在研究“轻量级Django - Elman和Lavin”一书,我在第4章和第5章中被困了好几天,我们应该使用rest框架和backbone.js。举个例子,

轻型Django -第4章和第5章

几天前,我尝试用myseld编写代码,如书中所示,并检查上面链接中的示例。但是,由于我没有继续,所以我决定复制上面的链接中的代码,并尝试运行。也出现了同样的错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
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框架。由于我使用的是上面链接中的确切代码,所以它应该没有错误。实际上,我更改的唯一代码是在settings.py中(在错误反复发生之后):

以前:

代码语言:javascript
复制
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scrum',
    }
}

Now:

代码语言:javascript
复制
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'scrum',
        'USER': 'daniel', 
        'PASSWORD': '12345',
        'HOST': '127.0.0.1',
        'PORT': '5432',        
    }

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

代码语言:javascript
复制
Role name |                   Attributes                   | Member of | Description 
-----------+------------------------------------------------+-----------+-------------
 daniel    | Superuser, Create DB                           | {}        | 
 postgres  | Superuser, Create role, Create DB, Replication | {}        | 

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

代码语言:javascript
复制
                                      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

有人能帮我发现这个问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-15 13:12:58

阅读DRF文档这里

在Version2.x中,如果使用ModelSerializer类,序列化类有时会自动确定queryset参数。 这种行为现在被用于可写关系字段的显式queryset参数替换为始终。

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

serializers.py中有这样一行:

代码语言:javascript
复制
assigned = serializers.SlugRelatedField(slug_field=User.USERNAME_FIELD, required=False)

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

票数 47
EN

Stack Overflow用户

发布于 2022-10-09 11:33:16

在我的例子中,read_only=True提供了帮助。

票数 -1
EN

Stack Overflow用户

发布于 2021-05-21 06:55:26

您需要从序列化程序中删除RelatedField,django.in不再支持它了--我的情况--它起作用了。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27484344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档