简体   繁体   中英

how to get a field from a model in django api view

i have to api views that use a model. i tried use headlines.objects.all() to get everything in the model and the second view is supposed to get only the title field from the model, i have tried filtering and i got a positional argument error. this is my views file.

from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from news.models import Headline
from news.api.serializers import *

@api_view(['GET',])
def api_detail(request, any):
    try:
        qs = Headline.objects.get(slug=any)
    except Headline.DoesNotExist:
        return Response(status = status.HTTP_404_NOT_FOUND)
    if request.method == "GET":
        serializer = HeadlineSerializer(qs)
        return Response(serializer.data)

@api_view(['GET',])
def api_head(request):
    try:
        py = Headline.objects.all().filter(title=title).order_by('-id')
    except Headline.DoesNotExist:
        return Response(status = status.HTTP_404_NOT_FOUND)
    if request.method == "GET":
        serializer = HeadlineSerializer(py, many=True)
        return Response(serializer.data)

This is my serializers.py file

from rest_framework import serializers
from news.models import Headline

class HeadlineSerializer(serializers.ModelSerializer):
    class Meta:
        model = Headline
        fields = ['title', 'contentt']

here is my urls.py

from django.urls import path
from news.api.views import *

app_name = 'news'

urlpatterns = [
    path('<slug:any>/', api_detail, name='details'),
    path('', api_head, name='api_head'),


]

The problem is at this line:

py = Headline.objects.all().filter(title=title).order_by('-id')

title is not a variable defined here.

If i understood you right you want to get just the field title, so you would get a response like:

[
     {
         "title": "lorem"
     },
     {
         "title": "ipsum"
     },
]

To achieve this you could create another serializer for that view.

class HeadlineSerializer(serializers.ModelSerializer):
    class Meta:
        model = Headline
        fields = ['title', 'contentt']  


class HeadlineTitleSerializer(serializers.ModelSerializer):
        class Meta:
            model = Headline
            #fields will filter you response for which fields you want to return in the response.
            fields = ['title']

In you view:

@api_view(['GET',])
def api_detail(request, any):
    try:
        qs = Headline.objects.get(slug=any)
    except Headline.DoesNotExist:
        return Response(status = status.HTTP_404_NOT_FOUND)
    if request.method == "GET":
        serializer = HeadlineSerializer(qs)
        return Response(serializer.data)

@api_view(['GET',])
def api_head(request):
    py = Headline.objects.all().order_by('-id')
    if request.method == "GET":
        serializer = HeadlineTitleSerializer(py, many=True)
        return Response(serializer.data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM