Я пытаюсь создать поле выбора веб-службы, используя Django rest framework. Соединение с моей базой данных работает, но когда я публикую свой веб-сервис, он выдаст мне ошибку: -: update()
должно быть реализовано. "
Я прилагаю свой код ниже
Моя панель инструментов / models.py:
from django.db import models
from django.contrib import admin
from django import forms
class Campus(models.Model):
campus_choices = (('Campus_1','Campus_1'),('Campus_2','Campus_2'),('Campus_3','Campus_3'))
select_campus = models.CharField(max_length = 100,choices = campus_choices)
class Building(models.Model):
building_choices = (('Building_1','Building_1'),('Building_2','Building_2'),('Building_3','Building_3'))
select_building = models.CharField(max_length = 100, choices = building_choices)
class Floor(models.Model):
floor_choices = (('Floor_1','Floor_1'),('Floor_2','Floor_2'),('Floor_3','Floor_3'))
select_floor = models.CharField(max_length = 100,choices = floor_choices)
class Rooms(models.Model):
room_choices = (('Room_1','Room_1'),('Room_2','Room_2'),('Room_3','Room_3'))
select_rooms = models.CharField(max_length = 100, choices = room_choices)
serializers.py
from rest_framework import serializers
from .models import *
from dashboard.models import Campus
class CampusSerializers(serializers.Serializer):
class Meta:
campus_choices = [('Campus_1','Campus_1'),('Campus_2','Campus_2'),('Campus_3','Campus_3')]
model = Campus
select_campus = serializers.ChoiceField(choices = campus_choices)
def create(self, validated_data):
campus_obj = Campus(**validated_data)
campus_obj.save()
return campus_obj
def update(self, instance, validated_data):
instance.select_campus = validated_data["select_campus"]
instance.save()
return instance
class BuildingSerializers(models.Model):
select_building = serializers.CharField(max_length = 100)
def create(self, validated_data):
building_obj = Building(**validated_data)
building_obj.save()
return building_obj
def update(self, instance, validated_data):
instance.select_building = validated_data["select_building"]
instance.save()
return instance
class FloorSerializers(models.Model):
select_floor = serializers.CharField(max_length = 100)
def create(self, validated_data):
floor_obj = Floor(**validated_data)
floor_obj.save()
return floor_obj
def update(self, instance, validated_data):
instance.select_floor = validated_data["select_floor"]
instance.save()
return instance
class RoomsSerializers(models.Model):
select_rooms = serializers.CharField(max_length = 100)
def create(self, validated_data):
room_obj = Floor(**validated_data)
room_obj.save()
return room_obj
def update(self, instance, validated_data):
instance.select_room = validated_data["select_room"]
instance.save()
return instance
views.py
from django.shortcuts import render
from dashboard.serializers import *
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .models import *
from rest_framework import status
from rest_framework.views import APIView
# Create your views here.
@api_view(['POST'])
def add_campus(request):
campus_serializer = CampusSerializer(data=request.data)
if campus_serializer.is_valid():
campus_serializer.save()
return Response({"data": "Campus added successfully"}, status=status.HTTP_201_CREATED)
else:
error_details = []
for key in campus_serializer.errors.keys():
error_details.append({"field": key, "message": campus_serializer.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
@api_view(['POST'])
def add_building(request):
building_serializer = BuildingSerializers(data=request.data)
if building_serializer.is_valid():
building_serializer.save()
return Response({"data": "Campus added successfully"}, status=status.HTTP_201_CREATED)
else:
error_details = []
for key in building_serializer.errors.keys():
error_details.append({"field": key, "message": building_serializer.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
@api_view(['POST'])
def add_floor(request):
floor_serializer = FloorSerializers(data=request.data)
if floor_serializer.is_valid():
floor_serializer.save()
return Response({"data": "Campus added successfully"}, status=status.HTTP_201_CREATED)
else:
error_details = []
for key in floor_serializer.errors.keys():
error_details.append({"field": key, "message": floor_serializer.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
@api_view(['POST'])
def add_rooms(request):
rooms_serializer = RoomsSerializers(data=request.data)
if rooms_serializer.is_valid():
rooms_serializer.save()
return Response({"data": "Campus added successfully"}, status=status.HTTP_201_CREATED)
else:
error_details = []
for key in rooms_serializer.errors.keys():
error_details.append({"field": key, "message": rooms_serializer.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
@api_view(['POST'])
def campus_details(request):
try:
dashboard = Campus.objects.all()
except Campus.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'POST':
serializer = CampusSerializers(dashboard, data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
else:
error_details = []
for key in serializer.errors.keys():
error_details.append({"field": key, "message": serializer.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
update= get_object_or_404(Campus)
form = CampusSerializers(request.POST or None, instance=update)
if form.is_valid():
form.save()
return redirect('campus_details')
elif request.method != 'POST':
return False
Это показывает мне ошибку NotImplimented вроде это
Это точная ошибка
Любая помощь приветствуется.