Skip to content

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

@sunnythakr

Description

@sunnythakr

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions