Rest frame
I am using Rest Framework Ember along with Django Rest Framework as my JSON API backend for my Ember app.
https://github.com/ngenworks/rest_framework_ember
I got the boot to work correctly with the resource_name = False flag. Here is my code below:
class DocumentViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows documents to be viewed or edited.
"""
queryset = Document.objects.all()
serializer_class = DocumentSerializer
# Side loading code for documents
resource_name = False
# renderer_classes = (JSONRenderer, BrowsableAPIRenderer)
def list(self, request, *args, **kwargs):
# import IPython
# IPython.embed()
data = {'document': []}
for doc in self.get_queryset():
data['document'].append(doc)
data['contacts'] = doc.contacts.all()
serializer = DocumentContactSerializer(data)
return Response(serializer.data)
This works the way you would like it to work.
The problem is that since I implemented this and overwritten the list () method on the ModelViewSet whenever a new object is created in POST, I get this error:
'NoneType' object has no attribute '__getitem__'
If I comment resource_name = False then POST works as expected again.
Do you know what could be causing this?
source to share
I faced the same problem. Our setup is also Ember + DRF. And I found a solution.
You can override the method create
like this:
def create(self, request):
self.resource_name = 'document'
data = request.DATA # returns the right querydict now
# do what you want
This way, you keep the lateral load when using resource_name = false
in cases other than create
.
source to share