Problem with create API using DRF and it separate methods

1,287 views
Skip to first unread message

Angel Rojas

unread,
Dec 12, 2017, 8:35:28 AM12/12/17
to Django REST framework
hello everybody

I can't create separate methods using DRF, working well when using LISTAPIVIEW, but show me error when using CREATEAPIVIEW and UPDATEAPIVIEW(error is methods post and put not allowed) my code is :

LIST ALL ACCOUNTS # working well

class AccountList(generics.ListAPIView):
queryset = Account.objects.all().order_by('-id')
serializer_class = AccountSerializer


DETAIL ACCOUNT WITH ID USER # working well

class AccountDetail(generics.ListAPIView):
serializer_class = AccountDetailSerializer

# get_object and get recover detail from user
def get_object(self, pk):
try:
user = User.objects.get(id=pk)
return Account.objects.filter(user_id=user.id)
except Account.DoesNotExist:
raise Http404

def get(self, request, pk, format=None):
account_id = self.get_object(pk)
serializer = AccountDetailSerializer(account_id, many=True)
return JsonResponse(serializer.data, safe=False)

But when I try create an account not working well, and show me error {'detail' : 'method post not allowed'}

class AccountCreate(generics.CreateAPIView):
# queryset = Account.objects.all().order_by('-id')
serializer_class = AccountSerializer

def _allowed_methods(self):
self.http_method_names.append("post")
return [m.upper() for m in self.http_method_names if hasattr(self, m)]

def post(request, *args, **kwargs):
return JsonResponse({'msg': 'her in create post method view '})


the same error with update or put {'detail' : 'method put not allowed'}, I try changed with method PUT and not working 


class AccountUpdate(generics.UpdateAPIView):
serializer_class = AccountUpdateSerializer
queryset = Account.objects.all()
permission_classes = (permissions.IsAuthenticated,)

# def get_context_data(self, **kwargs):
# context = super(AccountUpdate, self)
# context.update(self.extra_context)
# return context

def update(self):
return JsonResponse({'msg': 'here put view'})


my urls is here

urlpatterns = [
# accounts
url(r'^accounts/$', views.AccountList.as_view()),
url(r'^accounts/$', views.AccountCreate.as_view()),
url(r'^accounts/(?P<pk>[0-9]+)', views.AccountDetail.as_view()),
url(r'^accounts/update/(?P<pk>[0-9]+)', views.AccountUpdate.as_view()),
#url(r'^accounts/update/(?P<pk>[0-9]+)/$', views.AccountUpdate.as_view()), not working
]


please tell me how to create an API with separate methods from DRF.


thanks for your attention.






















































Norbert Mate

unread,
Dec 13, 2017, 5:25:46 AM12/13/17
to Django REST framework
Hi, one problem is that you use the same url pattern for the list and create. Because the list url is the first one the POST request will enter there and the listAPIView is read only:

url(r'^accounts/$', views.AccountList.as_view()),
url(r'^accounts/$', views.AccountCreate.as_view()),
You could use the ListCreateAPIView and have a single url (I prefer this one) or change the url for one of them.

For the update method I don't see the problem.

Regards,
Norbert.

Norbert Mate

unread,
Dec 13, 2017, 5:30:35 AM12/13/17
to Django REST framework
I think the best approach would be to create the urls in a REST way. You should use 2 urls only, 1 for creating a new object and retrieving the list of objects (ListCreateAPIView) and another one for single object manipulation (RetrieveUpdateDestroyAPIView). The urls would look like this:

url(r'^accounts/?$', views.AccountListView.as_view()),
url(r'^accounts/(?P<pk>[0-9]+)/?$', views.AccountView.as_view()),


Regards,
Norbert.


On Tuesday, December 12, 2017 at 3:35:28 PM UTC+2, Angel Rojas wrote:
Reply all
Reply to author
Forward
0 new messages