7
我想確保request.user只能發出POST請求來創建一個論壇主題,它們是作者。使用PUT和DELETE我可以通過使用has_object_permission
來實現這一目標,但是通過POST我無法做到這一點,我猜測是因爲該對象尚未創建。POST上的Django Rest框架對象級別權限
class TopicPermission(IsAuthenticatedOrReadOnly):
"""
Any user should be able to read topics but only authenticated
users should be able to create new topics. An owner or moderator
should be able to update a discussion or delete.
"""
def has_object_permission(self, request, view, obj):
if request.method in SAFE_METHODS:
return True
# Instance must have an attribute named `author` or moderator
return obj.author == request.user or request.user.forum_moderator
我該如何去核實POST請求中的request.user == obj.author
?
你有一個'你的序列化,你的目標,確保登頂的時候被設置爲當前用戶author'場目的?有比做一個權限檢查有更好的方法。 –
是的,這不是那個。它適用於PUT和DELETE,但使用POST has_object_permission不起作用。 – awwester
「不工作」是指「不叫」,「觸發錯誤」或「永不過去」?目前還不清楚你在你的問題中想做什麼,它[聞起來像是一個XY問題](http://meta.stackexchange.com/q/66377/159034)。 –