2021-12-11

Tech Programing

程式人小天地

django rest api deleting data from db

2 min read


Whenever I access data using django rest api it gets deleted. Getall seems to work but view detail or update deletes the data. When I create I get a “201 Created” but the information is not stored in db. I am using the django sql_lite db and even with superuser it still gets deleted.

I have checked my serializers, models and views for errors. I have to create data directly into the db. I have checked migrations to make sure they were up to date.

views.py

from django.shortcuts import render
from rest_framework import generics
from .serializers import RoomListSerializer, RoomDetailSerializer
from .models import Room

class RoomListAPIView(generics.ListAPIView):
    queryset = Room.objects.all()
    serializer_class = RoomListSerializer

class RoomRetrieveAPIView(generics.RetrieveAPIView):
    lookup_field = "id"
    queryset = Room.objects.all()
    serializer_class = RoomDetailSerializer

class RoomCreateAPIView(generics.CreateAPIView):
    queryset = Room.objects.all()
    print(queryset)
    serializer_class = RoomDetailSerializer

class RoomRetrieveUpdateAPIView(generics.RetrieveUpdateAPIView):
    lookup_field = "id"
    queryset = Room.objects.all()
    serializer_class = RoomDetailSerializer

class RoomDestroyAPIView(generics.DestroyAPIView):
    lookup_field = "id"
    queryset = Room.objects.all()

models.py

from django.db import models

class Room(models.Model):
    room_name = models.CharField(max_length=200, blank=False)
    description = models.TextField(blank=True)
    width = models.CharField(blank=False, default="5", max_length=10)
    length = models.CharField(blank=False, default="5", max_length=10)

    def __str__(self):
        return f"{self.room_name}: {self.width} by {self.length}"

serializers.py

from rest_framework import serializers
from .models import Room
from rest_framework.reverse import reverse

class RoomListSerializer(serializers.ModelSerializer):
    absolute = serializers.SerializerMethodField()

    class Meta:
        model = Room
        fields = [
            'id',
            'room_name',
            'description',
            'absolute',
        ]

    def get_absolute(self, obj):
        return reverse('room_detail', args=(obj.pk,))

class RoomDetailSerializer(serializers.ModelSerializer):
    update = serializers.SerializerMethodField()

    class Meta:
        model = Room
        fields = [
            'id',
            'room_name',
            'description',
            'width',
            'length',
            'update',
            'delete',
        ]

    def get_update(self, obj):
        return reverse('room_update', args=(obj.pk,))
    
    def get_delete(self, obj):
        return reverse('room_delete', args=(obj.pk,))

Thanks for reading. Any advice you can give would be appreciated. This one has really gotten me stumped and my searches have been able to give me answer.



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *