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.