Movatterモバイル変換


[0]ホーム

URL:


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 inQ&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 modelsclass 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 iofrom rest_framework.parsers import JSONParserfrom . serializers import StudentSerializerfrom rest_framework.renderers import JSONRendererfrom django.http import HttpResponsefrom django.views.decorators.csrf import csrf_exempt@csrf_exemptdef 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.pyfrom rest_framework import serializersfrom .models import Studentclass 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.pyfrom django.contrib import adminfrom django.urls import pathfrom apiApp import viewsurlpatterns = [    path('admin/', admin.site.urls),    path('stucreate/', views.student_create),]
APIimport requestsimport 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 toDjango REST Framework implementing thejsonapi.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 thesupport 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
@MuhammadFareed
Comment options

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

@divyarajsinhsindhav
Comment options

I also follow tutorials of Geeky Show and got this error

@MuhammadFareed
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)

@divyarajsinhsindhav
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
@Shahanees1998
Comment options

Thank you sir your solution is working

@suryargiri
Comment options

This helped me, thanks

@capsamson
Comment options

Thank you so much. That saved the day!

@RiyaAgrawalRocks
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
@enghish
Comment options

You saved my day. Thank you so much

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
13 participants
@sunnythakr@sliverc@MuhammadFareed@srajsunny@suryargiri@capsamson@Saugatkafley@KomalRastogi1@enghish@kharramahendra@Shahanees1998@divyarajsinhsindhav@RiyaAgrawalRocks
Converted from issue

This discussion was converted from issue #929 on April 20, 2021 10:11.


[8]ページ先頭

©2009-2025 Movatter.jp