I'm a beginner with the Django REST framework and am building an app that summarizes articles with headers. I'm trying to get information from the Article model to pass into the Summary one. I'm having trouble understanding the use of get_object_or_404 in this scenario (how to instantiate a model) and how I use the pk argument. I tried changing the status URL to status/<int:article_id> and passed article as the pk, but I get a 404 when running the POST request. I'm stuck on how to use that argument otherwise. How do I instantiate a model from my request by using get_object_or_404 properly?
views.py:
import json
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse
from rest_framework.decorators import api_view
from .models import *
@api_view(['POST'])
def get_summary(request, pk):
article = get_object_or_404(Article, pk=pk)
mdl = Summary()
return JsonResponse(mdl.preprocess(article.content), safe=False)
models.py:
# Article class
class Article(models.Model):
headings = models.CharField(max_length=200)
content = models.CharField(max_length=500000)
# Summary results class (so far)
class Summary(models.Model):
preprocessor = TextPreprocessor()
in_text = models.CharField(max_length=500000)
topics = models.CharField(max_length=200)
def preprocess(self, text):
return self.preprocessor.preprocess(text)
urls.py:
from django.urls import path, include
from rest_framework import routers
from .api import *
from .views import *
router = routers.DefaultRouter()
router.register('my_api', SummaryViewSet, 'api')
urlpatterns = [
path('api/', include(router.urls)),
path('status/', get_summary)
]