What we want to accomplish.
I want to get the data of Category table from postgresql using Django’s rest framework.
Problems/Unknown Issues
When I try to access the db using api, I get a ProgrammingError and am told that the table does not exist.
Error message
ProgrammingError at /api/CategoryModel/
relation "category" does not exist
LINE 1: ...ory"."categoryid", "category"."categoryname" FROM "category"
^
Request Method: GET
Request URL: http://localhost:8000/api/CategoryModel/
Django Version: 5.0.6
Exception Type: ProgrammingError
Exception Value:
relation "category" does not exist
LINE 1: ...ory"."categoryid", "category"."categoryname" FROM "category"
^
Exception Location: /usr/local/lib/python3.12/site-packages/django/db/backends/utils.py, line 105, in _execute
Python Executable: /usr/local/bin/python
Python Version: 3.12.4
Source code in question
from django.db import models
class Category(models.Model):
categoryid = models.AutoField(primary_key=True)
categoryname = models.CharField(max_length=255)
class Meta:
managed = False
db_table = "category"
from rest_framework.routers import DefaultRouter
from django.urls import path, include
from .apis import CategoryViewSet
from .apis import router
router = DefaultRouter()
router.register(r"CategoryModel", CategoryViewSet)
urlpatterns = [
path("", include(router.urls)),
path("api/", include(router.urls)),
]
from rest_framework import viewsets, routers
from django.db.models import Q
from .models import Category
from .serializers import CategorySerializer
class CategoryViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
router = routers.DefaultRouter()
router.register(r"Category", CategoryViewSet)
from rest_framework import serializers
from .models import Category
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ["categoryname"]
I did makemigrations and migrations.
Verified that the table exists in postgresql and that the names are the same, including case.
Verified that the application name is in INSTALLED_APP.
Verified that psycopg2 is installed.
I did the above and was still told there were no tables.
ぷらいべーとさん is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.