我有一個Django後端使用Tastypie來生成它的RESTful API。此API的端點不在該域的頂層;相反,它通過使用WSGIScriptAlias存在於「子文件夾」位置。大多數情況下,這很好。但是我發現當我的一個資源有ToManyField關係時,Tastypie會拋出一個錯誤。這是我的一些相關代碼。該WSGIScriptAlias行:Django Tastypie ToManyField沒有正確解析相關的URI
WSGIScriptAlias /english/rubric /var/webapps/django/rubric/apache/django.wsgi
片段從有問題的資源:
class TraitResource(ModelResource):
criteria = fields.ToManyField('rubric.rubric.api.CriterionResource', "criteria", related_name='trait', full = True)
class Meta:
queryset = Trait.objects.all()
authentication=SessionAuthentication()
list_allowed_methods=['get']
always_return_data=True
class CriterionResource(ModelResource):
class Meta:
queryset = Criterion.objects.all()
authentication=SessionAuthentication()
list_allowed_methods=['get']
always_return_data=True
我的urls.py:
v1_api = Api(api_name='v1')
v1_api.register(TraitResource())
v1_api.register(CriterionResource())
urlpatterns = patterns('',
(r'^login/$', 'django_cas.views.login'),
(r'^logout/$', 'django_cas.views.logout'),
(r'^api/', include(v1_api.urls))
)
從錯誤郵件摘錄:
{
error_message: "{'path': u'english/rubric/api/v1/rubric', 'tried': [[<RegexURLPattern None ^login/$>], [<RegexURLPattern None ^logout/$>], [<RegexURLResolver [<RegexURLPattern api_v1_top_level ^(?P<api_name>v1)/?$>, <RegexURLResolver [<RegexURLPattern api_dispatch_list ...
(它從那裏繼續列出所有已經嘗試過的網址,但未能找到匹配的網址)。
所以如果我理解正確,發生了什麼事情是因爲我的WSGIScriptAlias,相關對象報告他們的完整路徑作爲資源URI,但是當tastypie去獲取相關對象返回一個我已經調用過,它們不匹配urls.py中的任何內容(因爲它只需要相對於WSGI應用程序安裝點的url模式)。我已經通過臨時修改WSGIScriptAlias來驗證這種情況,所以API位於根目錄 - 然後它一切正常。那麼,爲了保持子文件夾的URL模式,我需要做些什麼?我已經在重寫prepend_urls和get_resource_uri的過程中玩過了一段時間,但是我沒有真正瞭解這些函數在資源類中的使用方式,所以我不確定這對我是否正確。提前致謝。
同樣的問題在這裏... – 2013-05-09 09:38:49