0

我无法解决这个错误......我正在使用restframework2分支。

我做错了什么,还是restframework2代码中的错误?

这是我的代码:

资源.py

class TemplateHoursSerializer(serializers.ModelSerializer):
    class Meta:
        model = TemplateHours
        nested = True

    start = HourField()
    end = HourField()
    employee = EmployeeSerializer()

class TemplateHoursResource(ModelResource):
    model = TemplateHours
    serializer_class = TemplateHoursSerializer

网址.py

url(r'^api/template-hours/$', TemplateHoursResource.as_view(actions={
    'get': 'list',
    'post': 'create'
})),
url(r'^api/template-hours/(?P<pk>[0-9]+)/$', TemplateHoursResource.as_view(actions={
    'get': 'retrieve',
    'put': 'update',
    'delete': 'destroy'
})),
...

当我访问(或 POST 到)http://127.0.0.1:8000/api/template-hours/时,我收到此错误并回溯:

AttributeError at /api/template-hours/
can't set attribute
Request Method: GET
Request URL:    http://127.0.0.1:8000/api/template-hours/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:    
can't set attribute
Exception Location: C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-    packages\rest_framework\resources.py in wrapped, line 13
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2
Python Path:    
['C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\Scripts',
 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c12dev_r88846-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\virtualenv-1.7.1.2-py2.7.egg',
 'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\Lib\\site-packages',
 'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\hedron',
 'C:\\Python27\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 path(u'C:\\Users\\Mathieu\\Development\\django_projects\\hedron'),
 path(u'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\hedron\\apps'),
 path(u'C:\\Users\\Mathieu\\Development\\django_projects\\hedron\\hedron\\libs')]
Server time:    di, 9 Okt 2012 22:46:50 +0200

Traceback:
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\django\core\handlers\base.py" in get_response
  111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in view
  48. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  77. return view_func(*args, **kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\views.py" in dispatch
  324. response = self.handle_exception(exc)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\views.py" in dispatch
  321. response = handler(request, *args, **kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in list
  74. return self.root_view().list(request, args, kwargs)
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in root_view
  68. return wrapped(self, self.root_class())
File "C:\Users\Mathieu\Development\django_projects\hedron\Lib\site-packages\rest_framework\resources.py" in wrapped
  13. setattr(dest, attr, getattr(source, attr))

Exception Type: AttributeError at /api/template-hours/
Exception Value: can't set attribute
4

1 回答 1

1

REST framework 2 [beta of] 中尚未完成/支持资源和路由器。它们上的文档是我希望设计看起来像的占位符,但我今天将从索引中删除它们。希望他们能在 2.0 中使用它,但我认为它根本不是必需的,因为您可以使用 Views 和显式 URLconfs 完成所需的一切。资源和路由器只是给你一个有用的捷径。

于 2012-10-10T08:26:28.390 回答