I've been trying for a while to get a ModelResource or a View working using the Django Rest Framework. I'm following the examples but the code in the examples is not working for me. Can anyone tell me why I might be getting this error.
views.py
# Create your views here.
from django.http import HttpResponse
from django.utils import simplejson
from django.core import serializers
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status
from interface.models import *
def TestView(View):
def get(self, request):
return Person.objects.all()
urls.py
from django.conf.urls.defaults import *
from djangorestframework.resources import ModelResource
from djangorestframework.views import ListOrCreateModelView, InstanceModelView, View
from interface.models import *
from interface.views import *
class PersonResource(ModelResource):
model = Person
ordering = ('LastName')
urlpatterns = patterns('',
url(r'^$', 'interface.views.index'),
url(r'^testview/$', TestView.as_view()),
url(r'^people/$', ListOrCreateModelView.as_view(resource=PersonResource)),
)
I'm now getting the error 'function' object has no attribute 'as_view'.
Since this is the #1 hit on google for this error message and there's a more subtle and probably common cause for it than the OPs, I'm posting this comment here.
This error can also be caused by using a standard view decorator on a class based view instead of on the __dispatch__
method within the view.
def TestView(View):
should be class TestView(View):
. As it stands, you define a function called TestView
which takes an argument called View
-- its body defines an inner function, then returns None
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With