![](/img/trans.png)
[英]Django: Django-Rest-Framework gives Attribution error (pagination)
[英]Assertion error at: Django-rest-Framework
我使用的是 python 3.4、Django 1.7.1(書中考慮的版本)、Postgres 9.3,我的 IDE 是 Eclipse。
我一直在學習“輕量級 Django - Elman 和 Lavin”這本書,我在第 4 章和第 5 章被困了好幾天,我們應該在那里使用其余框架和骨干.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-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
有人可以幫我發現問題嗎?
在此處閱讀 DRF 文檔。
在版本 2.xa 中,如果使用 ModelSerializer 類,序列化器類有時可以自動確定查詢集參數。
此行為現在被替換為始終對可寫關系字段使用顯式查詢集參數。
您只是使用比所用代碼作者更新的 DRF 版本,因此您需要使用較低版本或修復代碼。
在serializers.py 中有這一行:
assigned = serializers.SlugRelatedField(slug_field=User.USERNAME_FIELD, required=False)
您需要添加read_only=True
或queryset=User.objects.all()
您需要從序列化程序中刪除相關字段,django 不再支持它。在我的情況下,它起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.