2016-03-06 91 views
3

雖然學習Django的REST框架,我在/任務/ 1錯誤Django的Asse田在URL

預計視圖TaskDetail將與名爲「PK」的URL關鍵字參數調用得到了Asse田。修正你的URL conf,或者在視圖上正確設置.lookup_field屬性。

我model.py

class Task(models.Model): 
    owner=models.ForeignKey('auth.User',related_name='tasks') 
    completed=models.BooleanField(default=False) 
    title=models.CharField(max_length=100) 
    description=models.TextField() 

serializer.py

class TaskSerializer(serializers.ModelSerializer): 

class Meta: 
    model = Task 
    read_only=('owner.username',) 
    fields=('title','description','completed','owner.username') 

permission.py

class IsOwnerOrReadOnly(BasePermission): 
    def has_object_permission(self, request, view, obj): 
     if request.method is SAFE_METHODS: 
      return True 

     return obj.owner==request.user 

views.py

class TasksMixins(object): 
    queryset = Task.objects.all() 
    serializer_class=TaskSerializer 
    permission_classes=(IsOwnerOrReadOnly,) 

    def pre_save(self,obj): 
     obj.owner=self.request.user 

class TaskList(TasksMixins,ListCreateAPIView): 
    pass 

class TaskDetail(TasksMixins,RetrieveUpdateDestroyAPIView): 
    pass 

Urls.py

urlpatterns = [ 
    url(r'^$', views.home, name='home'), 
    url(r'^tasks/$', views.TaskList.as_view(), name='task_list'), 
    url(r'^tasks/(?P<id>[0-9]+)$', views.TaskDetail.as_view(), name='task_detail') 
] 

回溯

Traceback (most recent call last): 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view 
    return view_func(*args, **kwargs) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view 
    return self.dispatch(request, *args, **kwargs) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch 
    response = self.handle_exception(exc) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch 
    response = handler(request, *args, **kwargs) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/rest_framework/generics.py", line 286, in get 
    return self.retrieve(request, *args, **kwargs) 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 56, in retrieve 
    instance = self.get_object() 
    File "/home/amogh/PycharmProjects/env_1.9/local/lib/python2.7/site-packages/rest_framework/generics.py", line 93, in get_object 
    (self.__class__.__name__, lookup_url_kwarg) 
AssertionError: Expected view TaskDetail to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly. 

當過我瀏覽到的鏈接我得到這個錯誤

任何幫助深表感謝......感謝advace

error image

+1

在'urls.py',第4行,改變''到''。你正在傳遞一個'pk' kwarg,但是這個url正在等待一個'id' kwarg。這是Django慣例使用pk而不是id。 – Bjorn

+0

@Bjorn ....感謝您的回覆......但獲得**字段名稱'owner.username'對模型「Task」無效**錯誤 – Coeus

+0

因爲它不是'Task'字段。查看該字段的來源參數 – Linovia

回答

2

如果您想通過「PK」的目標,只是重命名ID - > PK到您的url.py:

url(r'^tasks/(?P<pk>[0-9]+)$', views.TaskDetail.as_view(), name='task_detail')

如果你想比其他的PK場的目標,,你必須調整網址.py,view.py和serializer.py精確的lookup_field(可以嵌套),例如,它可能適合你。

url.py:

url(r'^tasks/(?P<owner__username>[0-9]+)$', views.TaskDetail.as_view(), name='task_detail') 

view.py:

class TasksMixins(object): 
    queryset = Task.objects.all() 
    serializer_class=TaskSerializer 
    permission_classes=(IsOwnerOrReadOnly,) 
    lookup_field = 'owner__username' 

serializer.py

class TaskSerializer(serializers.ModelSerializer): 
    owner = serializers.SlugRelatedField(slug_field='username',many=False, read_only=True) 
    class Meta: 
     model = Task 
     fields='__all__' 
     lookup_field = 'owner__username'