Answer the question
In order to leave comments, you need to log in
Why is RetrieveUpdateDestroyAPIView not working?
#settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'posts.apps.PostsConfig',
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
]
}
#blog_project_urls
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url('api', include('posts.urls')),
url('admin/', admin.site.urls),
]
#post_url
from django.conf.urls import url
from .views import (
PostListView,
PostDetailView
)
urlpatterns=[
url('/<int:pk>/', PostDetailView.as_view()),
url(' ',PostListView.as_view())
]
#post_view
from django.shortcuts import render
from .serializers import PostSerializer
# Create your views here.
from .models import Posts
from rest_framework import generics
class PostListView(generics.ListCreateAPIView):
queryset=Posts.objects.all()
serializer_class=PostSerializer
class PostDetailView(generics.RetrieveUpdateDestroyAPIView):
queryset=Posts.objects.all()
serializer_class=PostSerializer
# post_serializers
from rest_framework import serializers
from .models import Posts
class PostSerializer(serializers.ModelSerializer):
class Meta:
model=Posts
fields=('id', 'author', 'title', 'body', 'created_at',)
#post_models
from django.db import models
from django.contrib.auth.models import User
class Posts(models.Model):
author=models.ForeignKey(
User,
on_delete=models.CASCADE
)
title=models.CharField(max_length=200)
body=models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question