OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Django REST ModelViewSet view returns 404 after PATCH

  • Thread starter Thread starter user25455142
  • Start date Start date
U

user25455142

Guest
I have ModelViewSet view in django rest framework project. I am trying to filter documents that are not live, I mean are not on main view. To filter I am using params: http://localhost:8000/documents?is_live=false. I have list of records. After set true on one of objects I'm getting 404 error

Code:
"GET /api/documents/?is_live=false HTTP/1.1" 200
test {'pk': '2', 'partial': True}
Not Found: /api/documents/2/
Not Found: /api/documents/2/
"PATCH /api/documents/2/ HTTP/1.1" 404
"GET /api/documents/?is_live=false HTTP/1.1" 200

My view's code and custom filter (django_filters) are below:

Code:
class DocumentFilter(dfilters.FilterSet):
    is_live = dfilters.BooleanFilter(field_name='is_live')

    class Meta:
        model = Document
        fields = ['is_live']


class DocumentViewSet(viewsets.ModelViewSet):
    serializer_class = DocumentSerializer
    queryset = Document.objects.all()
    filterset_class = DocumentFilter
    filter_backends = [filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend]
    search_fields = ['title', 'category']
    ordering_fields = [
        'title',
        'category',
    ]

    def update(self, request, *args, **kwargs):
        print("test", args, kwargs)
        kwargs['partial'] = True
        return super().update(request, *args, **kwargs)

    @action(detail=True, methods=['patch'])
    def get_queryset(self):
        if self.request.query_params.get('is_live'):
            return self.queryset.filter(is_live=False)
        return self.queryset.filter(is_live=True)

EDIT

models.py


Code:
class Document(models.Model):
    title = models.CharField(max_length=100,blank=True)
    description = models.CharField(max_length=1000, blank=True)
    is_live = models.BooleanField(default=True)

    class Meta:
        verbose_name = "Document"
        verbose_name_plural = "Documents"

    def __str__(self):
        return self.title

project.urls

Code:
urlpatterns = [
    ...
    path('api/', include('api.urls')),
    ...
]

api.url

Code:
apirouter = routers.DefaultRouter()
...
apirouter.register('documents', DocumentViewSet)
...

I would like to have possibility to partial update of object on filtered view.
<p>I have ModelViewSet view in django rest framework project. I am trying to filter documents that are not live, I mean are not on main view. To filter I am using params: <code>http://localhost:8000/documents?is_live=false</code>. I have list of records. After set true on one of objects I'm getting 404 error</p>
<pre><code>
"GET /api/documents/?is_live=false HTTP/1.1" 200
test {'pk': '2', 'partial': True}
Not Found: /api/documents/2/
Not Found: /api/documents/2/
"PATCH /api/documents/2/ HTTP/1.1" 404
"GET /api/documents/?is_live=false HTTP/1.1" 200
</code></pre>
<p>My view's code and custom filter (django_filters) are below:</p>
<pre><code>class DocumentFilter(dfilters.FilterSet):
is_live = dfilters.BooleanFilter(field_name='is_live')

class Meta:
model = Document
fields = ['is_live']


class DocumentViewSet(viewsets.ModelViewSet):
serializer_class = DocumentSerializer
queryset = Document.objects.all()
filterset_class = DocumentFilter
filter_backends = [filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend]
search_fields = ['title', 'category']
ordering_fields = [
'title',
'category',
]

def update(self, request, *args, **kwargs):
print("test", args, kwargs)
kwargs['partial'] = True
return super().update(request, *args, **kwargs)

@action(detail=True, methods=['patch'])
def get_queryset(self):
if self.request.query_params.get('is_live'):
return self.queryset.filter(is_live=False)
return self.queryset.filter(is_live=True)
</code></pre>
<p><strong>EDIT</strong></p>
<p><strong>models.py</strong></p>
<pre><code>class Document(models.Model):
title = models.CharField(max_length=100,blank=True)
description = models.CharField(max_length=1000, blank=True)
is_live = models.BooleanField(default=True)

class Meta:
verbose_name = "Document"
verbose_name_plural = "Documents"

def __str__(self):
return self.title
</code></pre>
<p><strong>project.urls</strong></p>
<pre><code>urlpatterns = [
...
path('api/', include('api.urls')),
...
]
</code></pre>
<p><strong>api.url</strong></p>
<pre><code>apirouter = routers.DefaultRouter()
...
apirouter.register('documents', DocumentViewSet)
...
</code></pre>
<p>I would like to have possibility to partial update of object on filtered view.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top