我使用python3.4,Django 1.7.1 (书中考虑的版本),Postgres 9.3,我的IDE是Eclipse。
我一直在研究“轻量级Django - Elman和Lavin”一书,我在第4章和第5章中被困了好几天,我们应该使用rest框架和backbone.js。举个例子,
几天前,我尝试用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框架。由于我使用的是上面链接中的确切代码,所以它应该没有错误。实际上,我更改的唯一代码是在settings.py中(在错误反复发生之后):
以前:
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',
}如下所示,我的用户"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有人能帮我发现这个问题吗?
发布于 2014-12-15 13:12:58
阅读DRF文档这里。
在Version2.x中,如果使用ModelSerializer类,序列化类有时会自动确定queryset参数。 这种行为现在被用于可写关系字段的显式queryset参数替换为始终。
您只是在使用比所使用代码的作者更新的DRF版本,所以您需要使用更低的版本或修复代码。
在serializers.py中有这样一行:
assigned = serializers.SlugRelatedField(slug_field=User.USERNAME_FIELD, required=False)您需要添加read_only=True或queryset=User.objects.all()
发布于 2022-10-09 11:33:16
在我的例子中,read_only=True提供了帮助。
发布于 2021-05-21 06:55:26
您需要从序列化程序中删除RelatedField,django.in不再支持它了--我的情况--它起作用了。
https://stackoverflow.com/questions/27484344
复制相似问题