Django Rest Framework viewset not returning correct data from queryset

Viewed 492

I am pretty new to Django Rest Framework and am trying to build an API that has various viewsets, and almost all are working properly except one.

In my viewset I have 3 ModelViewSet's: one to list all Inspections, one to show all completed (done) inspections and the last one to show all the undone inspections. The problem is that it is returning the list of all inspections correctly, but the other 2 return "detail: not found" even though I have instances of Inspections in my database.

Here is my Inspection Viewset:

from rest_framework.viewsets import ModelViewSet
from inspections.models import Inspection
from .serializers import InspectionSerializer
from rest_framework import generics

class InspectionViewSet(ModelViewSet):
    queryset = Inspection.objects.all()
    serializer_class = InspectionSerializer


class DoneInspectionsViewSet(ModelViewSet):
    serializer_class = InspectionSerializer

    def get_queryset(self):
        queryset = Inspection.objects.all()
        return queryset


class UndoneInspectionsViewSet(ModelViewSet):
    serializer_class = InspectionSerializer

    def get_queryset(self):
        queryset = Inspection.objects.filter(done=False)
        return queryset

Here's my Inspection Serializer:

from rest_framework.serializers import ModelSerializer
from inspections.models import Inspection
from properties.api.serializers import PropertySerializer

class InspectionSerializer(ModelSerializer):
    property = PropertySerializer(many=False)
    class Meta:
        model = Inspection
        fields = ('id', 'connected_check', 'category', 'property', 'date',
                  'done_date', 'done_by', 'staff', 'notes', 'done')

Here's the Inspection model:

from django.db import models
from check.models import Check
from properties.models import Property
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone

class Inspection(models.Model):
    connected_check = models.ForeignKey(Check, on_delete=models.DO_NOTHING, null=True, blank=True, related_name='inspection_check')
    category = models.ForeignKey(InspectionCategory, on_delete=models.DO_NOTHING)
    property = models.ForeignKey(Property, on_delete=models.DO_NOTHING, related_name='inspection_property')
    date = models.DateField()
    done_date = models.DateField(blank=True, null=True)
    done_by = models.ForeignKey(User, max_length=25, blank=True, null=True, related_name='inspection_done_by', on_delete=models.DO_NOTHING)
    staff = models.ForeignKey(User, max_length=25, null=True, blank=True, related_name='inspection_staff', on_delete=models.DO_NOTHING)
    notes = models.TextField(max_length=500, blank=True)
    done = models.BooleanField(default=False)

    def __str__(self):
        return self.category.name

    def get_absolute_url(self):
        return reverse("inspections:details",kwargs={'pk':self.pk})

    def get_category_name(self):
        return self.category

    def is_past_due(self):
        return timezone.now() > self.date

    def is_done(self):
        self.done = True
        self.done_date = timezone.now()
        self.save()

And here are the urlpatterns and router:

router = routers.DefaultRouter(trailing_slash=False)
router.register(r'inspection', InspectionViewSet)
router.register(r'inspection/done', DoneInspectionsViewSet, basename='done-inspection')
router.register(r'inspection/undone', UndoneInspectionsViewSet, basename='undone-inspection')

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^admin/', admin.site.urls),
    url(r'^inspections/', include(('inspections.urls','inspections'), namespace='inspections')),
]

urlpatterns += [
    path('api/', include(router.urls)),
    path('api/schema/', get_schema_view(
        title = "DAPIMS",
        description = "API for DAPIMS",
        version = "1.0.0"
    ), name='openapi-schema'),
]

I have already tried to change the queryset and noticed that even if I just want to return all Inspection objects it still returns "detail: not found" even though if I run the interactive shell and execute the same queryset it returns the correct objects.

Here's an example of the return from the interactive shell

And this is what my Browsable API is returning

Thanks in advance!

0 Answers
Related