-
Notifications
You must be signed in to change notification settings - Fork 300
raise NotImplementedError('create() must be implemented.') NotImplementedError: create() must be implemented.
#930
-
I am Django REST API beginner research lot and trying various ways to solve but it not solve, please anyone can help me in this
here is my code
Create your models here.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=111)
roll = models.IntegerField()
city = models.CharField(max_length=111)
from django.shortcuts import render
# Create your views here.
import io
from rest_framework.parsers import JSONParser
from . serializers import StudentSerializer
from rest_framework.renderers import JSONRenderer
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def student_create(request):
if request.method =="POST":
json_data = request.body
stream = io.BytesIO(json_data)
pythondata = JSONParser().parse(stream)
serializer = StudentSerializer(data=pythondata)
if serializer.is_valid():
serializer.save()
res = {'msg':'data created'}
json_data = JSONRenderer().render(res)
return HttpResponse(json_data, content_type='application/json')
json_data = JSONRenderer().render(serializer.errors)
return HttpResponse(json_data, content_type='application/json')
serializers.py
from rest_framework import serializers
from .models import Student
class StudentSerializer(serializers.Serializer):
name = serializers.CharField(max_length=111)
roll = serializers.IntegerField()
city = serializers.CharField(max_length=722)
def create(self,validated_data):
return Student.objects.create(**validated_data)
urls.py
from django.contrib import admin
from django.urls import path
from apiApp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('stucreate/', views.student_create),
]
API
import requests
import json
URL = "http://127.0.0.1:8000/stucreate/"
data = {
'name':'sonam',
'roll':101,
'city':"Ranchi",
}
json_data = json.dumps(data)
r = requests.post(url = URL, data = json_data)
data = r.json()
print(data)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 5 comments 9 replies
-
Django REST Framework JSON API is an addon to Django REST Framework implementing the jsonapi.org spec. When I look at your example code it seems though you are using pure Django REST Framework. Therefore you should try to reach out to one of the support channels of Django REST Framework. They should be able to assist you.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi there I think you have also stumbled upon Geeky Shows Rest_api tutorials , I am also stucked in the same problem of unable to create model and
JSON parse errror at line 1
...? Did you fix that one ? I really need help on that , I am also new in Django as well as REST api .
I have made GET functions but could not fix POST and PUT
Beta Was this translation helpful? Give feedback.
All reactions
-
Same happened with me! I was also following tutorials of Geeky Show and got this error! Any solution for this error?
Beta Was this translation helpful? Give feedback.
All reactions
-
I also follow tutorials of Geeky Show and got this error
Beta Was this translation helpful? Give feedback.
All reactions
-
I also follow tutorials of Geeky Show and got this error
Check indentation of (def create)
def create(self,validated_data):
return Student.objects.create(**validated_data)
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, It's work
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi,
Please check your indentation, that 'create' function should be inside that Serializer class, as below
class StudentSerializer(serializers.Serializer):
name = serializers.CharField(max_length=111)
roll = serializers.IntegerField()
city = serializers.CharField(max_length=722)
def create(self,validated_data):
return Student.objects.create(**validated_data)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 5 -
😄 2 -
❤️ 6
-
Thank you sir your solution is working
Beta Was this translation helpful? Give feedback.
All reactions
-
This helped me, thanks
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you so much. That saved the day!
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you, this solved my issue
Beta Was this translation helpful? Give feedback.
All reactions
-
please check everywhere that any spelling mistake
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
instead of using serializers.Serializer use , serializers.ModelSerializer, it works for me .
Beta Was this translation helpful? Give feedback.
All reactions
-
😄 3
-
You saved my day. Thank you so much
Beta Was this translation helpful? Give feedback.