How to return values that are NULL in database with Django?

I hava a django app and I try to write some api calls.

So I have one table And I want to return the values where category_id is null.

So this sql statement works:

SELECT * FROM public.djangoadmin_category
where djangoadmin_category.category_id is null 

returns as output:

11  "zoogdieren"    "zoogdieren"    "hoi"   "photos/categories/1_eDJtmdP.jpg"   "2023-01-27 18:25:18.624272+01" "2023-01-27 18:25:18.624272+01" 
12  "amfibieen" "amfibieen" "kujhkjh"   "photos/categories/1_KJDTBPc.jpg"   "2023-01-27 18:25:38.444066+01" "2023-01-27 18:25:38.444066+01" 
13  "vogels"    "vogels"    "kljhkjh"   "photos/categories/1_FGkA44b.jpg"   "2023-01-27 18:26:00.390812+01" "2023-01-27 18:26:00.390812+01" 
21  "reptielen" "reptielen" "reptielen" "photos/categories/1_EoVggfL.jpg"   "2023-01-27 18:55:04.565339+01" "2023-01-27 18:55:04.565339+01" 
23  "schildpadden"  "schildpadden"  "schildpadden"  "photos/categories/1_RkKQ5md.jpg"   "2023-01-27 18:55:51.724641+01" "2023-01-29 12:40:45.014174+01" 

But now I want to write a API call for it, so that I can use it for the frontend.

the model:

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    description = models.TextField(max_length=1000)
    images = models.ImageField(upload_to="photos/categories")
    category = models.ForeignKey(
        "Category", on_delete=models.CASCADE, related_name='part_of', blank=True, null=True)
    date_create = models.DateTimeField(auto_now_add=True)
    date_update = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "category"
        verbose_name_plural = "categories"

    def __str__(self):

from django.urls import path, include
from rest_framework import routers
from .views import CategoryViewSet

router = routers.DefaultRouter()
router.register('categories', CategoryViewSet)

urlpatterns = [
    path('', include(router.urls))


from rest_framework import serializers
from .models import Animal, Category
class CategorySerializer(serializers.ModelSerializer):
    animals = AnimalSerializer(many=True)
    class Meta:
        model = Category
        fields = ['id','name', 'description', 'animals']

And the api method:

from rest_framework import viewsets, status
from rest_framework.decorators import action
from django.http import HttpResponse
from .serializers import CategorySerializer
from .models import Category

class CategoryViewSet(viewsets.ModelViewSet):
    serializer_class = CategorySerializer
    queryset = Category.objects.all()
    @action(detail=True, methods=['GET'])
    def main_groups(self, request, pk=None):
        return_groups = Category.objects.filter(category_id__isnull=True ).values()
        return  HttpResponse(return_groups, status= status.HTTP_200_OK)

and the url I am using is this one:

But this returns as output:

    "detail": "Not found."

Back to Top