Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

406
Visualizações
UpdateAPIView not working: Method "PATCH" not allowed

I use Django along with Django REST Framework to implement a RESTful API.

What I have right now:

  • A ContentEntry model
  • A ContentEntryCreateUpdateSerializer serializer for my ContentEntry model
  • A ContentEntryCreate view to create some ContentEntryies
  • A ContentEntryUpdate view to update the ContentEntryies

Here is the code:

from django.db import models
from rest_framework import serializers
from rest_framework import generics
from rest_framework.views import APIView
from my_api_app import views


# models.py
class ContentEntry(models.Model):
    content = models.ForeignKey(Content)
    quantity = models.IntegerField()
    container = models.ForeignKey(Container, related_name='content_entries')


# serializers.py
class ContentEntryCreateUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = ContentEntry
        fields = ('id', 'content', 'quantity', 'container')


# views.py
class ContentEntryCreate(generics.CreateAPIView):
    queryset = ContentEntry.objects.all()
    serializer_class = ContentEntryCreateUpdateSerializer


# views.py
class ContentEntryUpdate(generics.UpdateAPIView):
    queryset = ContentEntry.objects.all()
    lookup_field = 'id'
    serializer_class = ContentEntryCreateUpdateSerializer


# urls.py
urlpatterns = [
    url(r'content-entry', views.ContentEntryCreate.as_view()),
    url(r'content-entry/(?P<id>\d+)$', views.ContentEntryUpdate.as_view()),
]

Everything is working fine except the ContentEntryUpdate which always returns an error:

HTTP/1.1 405 Method Not Allowed
Allow: POST, OPTIONS
Content-Type: application/json
Date: Wed, 03 May 2017 14:40:03 GMT
Server: WSGIServer/0.2 CPython/3.6.1
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{"detail":"Method \"PATCH\" not allowed."}

As you can see in the Allow attribute, it seems that only the POST and OPTIONS methods are allowed by the server.

It's very strange since generics.UpdateAPIView defines the put and patch methods.

I don't think it's a permission problem since I allow everything:

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny'
    ]
}

What should I do to allow the HTTP PATCH and PUT methods?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Please make sure to mark the URLs with a start and end (^ and $ symbols).

What happens here is without the end mark, r'content-entry' matches "/content-entry/4/" and therefore calls the creation view.

Use: r'^content-entry$' and r'^content-entry/(?P<id>\d+)$' instead.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda