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:
>
> 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.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to