Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

raise NotImplementedError('create() must be implemented.') NotImplementedError: create() must be implemented. #930

Unanswered
sunnythakr asked this question in Q&A
Discussion options

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)
You must be logged in to vote

Replies: 5 comments 9 replies

Comment options

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.

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
4 replies
Comment options

Same happened with me! I was also following tutorials of Geeky Show and got this error! Any solution for this error?

Comment options

I also follow tutorials of Geeky Show and got this error

Comment options

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)

Comment options

Yes, It's work

Comment options

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)
You must be logged in to vote
4 replies
Comment options

Thank you sir your solution is working

Comment options

This helped me, thanks

Comment options

Thank you so much. That saved the day!

Comment options

Thank you, this solved my issue

Comment options

please check everywhere that any spelling mistake

You must be logged in to vote
0 replies
Comment options

instead of using serializers.Serializer use , serializers.ModelSerializer, it works for me .

You must be logged in to vote
1 reply
Comment options

You saved my day. Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /